Syntax and Tags


PHP opening and closing Tags syntax

There are four different pairs of opening and closing tags which can be used in php. Here is the list of tags.

  • Default syntax
  • Short open Tags
  • HTML Script Tags
  • ASP Style Tags


Default Syntax

The default syntax starts with "<? php" and ends with "?>".

Example :

  1. echo "Default Syntax";  
  2. ?>  

Short open Tags

The short tags starts with "<?" and ends with "?>". Short style tags are only available when they are enabled in php.ini configuration file on servers.

Example :

  1. <?  
  2. echo "PHP example with short-tags";  
  3. ?>  

HTML Script Tags

HTML script tags look like this :

  1. <script language="php">  
  2. echo "This is HTML script tags.";  
  3. </script>  

Some editors like Front Page editor have own problem to deal with escape situation and the said script is effective to solve it.

ASP Style Tags

The ASP style tags starts with "<%" and ends with "%>". ASP style tags are only available when they are enabled in php.ini configuration file on servers.

Example :

  1. <%   
  2. echo 'This is ASP like style';  
  3. %>  

Note : The above two tags and examples are given only for reference, but not used in practice any more.

PHP Statement separation

In PHP, statements are terminated by semicolon (;) like C or Perl. The closing tag of a block of PHP code automatically implies a semicolon, there is no need to have a semicolon terminating the last line of a PHP block.

Rules for statement separation

  • a semicolon
  • AND/OR
  • a closing PHP tag

Valid Codes

  1. <?php  
  2. echo 'This is a test string';  
  3. ?>  

In the above example both semicolon(;) and a closing PHP tag are present.

  1. <?php echo 'This is a test' ?>   

In the above example there is no semicolon(;) after the last instruction but a closing PHP tag is present.

<?php echo 'Here the last closing tag is omitted' ;

In the above example there is a semicolon(;) in the last instruction but there is no closing PHP tag.

PHP Case sensitivity

In PHP the user defined functions, classes, core language keywords (for example if, else, while, echo etc.) are case-insensitive. Therefore the three echo statements in the following example are equal.

Example - 1

  1. <?php  
  2. echo("We are learning PHP case sensitivity <br />");   
  3. ECHO("We are learning PHP case sensitivity <br />");  
  4. EcHo("We are learning PHP case sensitivity <br />");  
  5. ?>   


We are learning PHP case sensitivity 
We are learning PHP case sensitivity 
We are learning PHP case sensitivity


On the other hand all variables are case-sensitive.

Consider the following example. Only the first statement display the value as $amount because $amount, $AMOUNT, $amoUNT are three different variables.

Example - 2

  1. <?php  
  2. $amount = 200;  
  3. echo("The Amount is : $amount <br />");   
  4. echo("The Amount is : $AMOUNT <br />");  
  5. echo("The Amount is : $amoUNT <br />");  
  6. ?>  


The Amount is : 200 
The Amount is : 
The Amount is : 


PHP whitespace insensitivity

In general whitespace is not visible on the screen, including spaces, tabs, and end-of-line characters i.e. carriage returns. In PHP whitespace doesn't matter in coding. You can break a single line statement to any number of lines or number of separate statements together on a single line.

The following two examples are same :

Example :

  1. <?php  
  2. function student_info($student_name$class$roll_no)  
  3. {  
  4. echo "The Name of student is : $student_name <br />";  
  5. echo "His Class is : $class and Roll No. is $roll_no";   
  6. }  
  7. student_info("David Rayy""V", 12)  
  8. ?>   


The Name of student is : David Rayy 
His Class is : V and Roll No. is 12


Example : Advance whitespace insensitivity

  1. <?php  
  2. function student_info  
  3. (  
  4. $student_name,  
  5. $class,  
  6. $roll_no  
  7. )  
  8. {  
  9. echo "The Name of student is : $student_name <br />";  
  10. echo "His Class is : $class and Roll No. is $roll_no";  
  11. }  
  12. student_info(  
  13. "David Rayy""V", 12  
  14. )  
  15. ?>  


The Name of student is : David Rayy 
His Class is : V and Roll No. is 12


Example : Whitespace insensitivity with tabs and spaces

In the following example spaces and tabs are used in a numeric operation, but in both cases $xys returns same value.

  1. <?php  
  2. // single space between $xyz, =, 11, +, 12  
  3. $xyz = 11 + 12;  
  4. echo $xyz.'<br />';  
  5. // tabs and spaces  
  6. $xyz =   11  +   12;  
  7. echo $xyz;  
  8. ?>  




PHP : Single line and Multiple lines Comments

Single line comment

PHP supports the following two different way of commenting.

# This is a single line comment.

//This is another way of single line comment.

Example :

  1. <?php  
  2. echo "How to make single line comment.";  
  3. # This is a single line comment.  
  4. //This is another way of single line comment.  
  5. ?>   


How to make single line comment.


Multiple lines comments

PHP supports 'C', style comments. A comment starts with the character pair /* and terminates with the character pair */.

/* This is a multiple comment testing,
and these lines will ignored
at the time of execution */

Example :

  1. <?php  
  2. echo "How to make multiline comments";  
  3. /* These are  a multiline comments 
  4. testing, and these lines will ignored 
  5. at the time of execution */  
  6. ?>   


How to make multiline comments


Multiple lines comments can not be nested

  1. <?php  
  2. echo "How to make multiline comments";  
  3.   /* This is a multiline comment 
  4.        testing, and these lines will ignored 
  5. at the time of execution  */  
  6. echo "Following multiline comments formats are wrong.";  
  7.   /* This is a multiline comment /* 
  8.        testing, and these lines will ignored */  
  9. at the time of execution  */  
  10. ?>  

First PHP Script

Here is the first PHP script which will display "Hello World..." in the web browser.

  1. <?php  
  2. echo "Hello World...";  
  3. ?>  

The tags tell the web server to treat everything inside the tags as PHP code to run. The code is very simple. It uses an in-build PHP function "echo" to display the text "Hello World ..." in the web page. Everything outside these tags are sent direct to the browser.

Pictorial presentation

Pictorial representation of First PHP Page

Combining PHP and HTML

PHP syntax is applicable only within PHP tags.

PHP can be embedded in HTML and placed anywhere in the document.

When PHP is embedded in HTML documents and PHP parses this document it interpreted the section enclosed with opening tag (<?php) and closing tag (?>) of PHP and ignore the rest parts of the document.

PHP and HTML are seen together in following example.

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
  2. "">  
  3. <html xmlns="">  
  4. <head>  
  5. <title> PHP Page</title>  
  6. </head>  
  7. <body>  
  8. <?php  
  9. echo "Hello World...";  
  10. echo "Hello World...";   
  11. ?>   
  12. </body>  
  13. </html>