We now have a youtube channel. Subscribe!

PHP Basic Syntax

PHP Basic Syntax

Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we will be studying about the PHP Syntax.

This tutorial gives you an idea of the basic syntax of PHP, and very important to make your foundation in PHP very strong.

Escaping to PHP

The PHP parsing engine needs a method to differentiate PHP code from other elements in the webpage. The process for doing so is known as 'escaping to PHP'. There are four ways to do this -

1. Cononical PHP Tags

The most universal efficient PHP tag style is -


If this style is used, you can be positive that your tags will always be well interpreted.

2. Short-open (SGML-style) Tags

Short-open tags look like this -


The short tags are, as one might expect, the shortest option. You must do one of the two things to enable PHP recognize the tags -

  • Select the --enable-short-tags config option when you are building PHP.
  • Set the short_open_tag option in your php.ini file to On. This option must be turned Off in order to parse XML with PHP because the same syntax is used for XML tags.

3. ASP-style Tags

ASP-style tags copy the tags used by Active Server Pages to describe code blocks. ASP-style tags look like this -


To use ASP-style tags, you will need to set the configuration option in the php.ini file.

4. HTML Script Tags

HTML script tags look like this -

<script language = "PHP">...</script>

Commenting PHP Code

Commenting PHP Code

A comment is the portion of a program that exists only for human readers and stripped out before displaying the program's output. There are two forms of commenting in PHP -

Single-Line Comments

Single-line comments are usually used for short explanations or notes relevant to the code. Following are the examples of single line comments -

   # This is a comment, and
   # This is the second line of the comment
   // This is a comment too. Each style comments only
   print "An example with single line comments";

Multi-Lines Printing

Following below are the examples to print multiple lines in a single line statement -

   # First Example
   print <<<END
   This uses the "here document" syntax to output
   multiple lines with $variable interpolation. Note
   that the here document terminator must appear on a
   line with just a semicolon no extra whitespace!
   # Second Example
   print "This spans
   multiple lines. The newlines will be
   output as well";

Multi-Lines Comments

Multi-lines comments are normally used for providing more detailed explanations when needed. The multi-line style of commenting is the same as in C. Here are the examples of multi-lines comments -

   /* This is a comment with multiline
      Author : Web Design Tutorialz Team
      Purpose: Multiline Comments Demo
      Subject: PHP
   print "An example with multi line comments";

PHP is Whitespace Insensitive

Whitespace is what you type that's typically invincible on the screen, including carriage returns, spaces, and tabs.

PHP whitespace insensitivity means that its almost never matters how many whitespace character you have in a row. One whitespace character is same as many such characters.


Each of the following below PHP statement that assigns the sum of 2 + 2 to the variable $four is equivalent -

$four = 2 + 2; // single spaces
$four <tab>=<tab2<tab>+<tab>2 ; // spaces and tabs
$four =
2; // multiple lines

PHP is Case Sensitive

PHP is a case sensitive language.


Try out the following example below -

         $capital = 80;
         print("Variable capital is $capital<br>");
         print("Variable CaPiTaL is $CaPiTaL<br>");


When the above code is executed, it will produce the following result -

Variable capital is 80
Variable CaPiTaL is

Statements are expressions terminated by semicolons

A statement in PHP is an expression that is followed by a semicolon (;). Any sequence of valid PHP statements that is enclosed by PHP tags is a valid program.


Here is a typical statement in PHP, which in this case assigns a string of characters to a variable that is called $greeting -

$greeting = "Welcome to PHP!";

Expressions are Combinations of Tokens

The smallest building blocks of PHP are the indivisible tokens, such as numbers (6.141), strings (.three.), variables ($four), constants (TRUE), and the special words that make up the syntax of PHP itself such as if, for, else, while, etc.

Braces make Blocks

Although statement can not be merged like expressions, you can always put an order of statements anywhere a statement can go by enclosing them in a set of curly braces.


Here both statements are equal -

if (3 == 2 + 1)
   print("Good - I haven't totally lost my mind.<br>");
if (3 == 2 + 1) {
   print("Good - I haven't totally");
   print("lost my mind.<br>");

Running PHP Script from Command Prompt

It's possible to run your PHP script on your command prompt. Assuming you have the following content in test.php file -

   echo "Hello PHP!!!!!";

Now run the PHP script from a command prompt as follows -

$ php test.php


When the above code is executed, it will produce the following result -

Hello PHP!!!!!

Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP Variable Types.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain