![PHP Tutorial](http://www.w3resource.com/w3r_images/php.gif)
There are four different pairs of opening and closing tags which can be used in php. Here is the list of tags.
Default Syntax
The default syntax starts with "<? php" and ends with "?>".
Example :
<?php echo "Default Syntax"; ?>
View the example (with default tag syntax) in the browser.
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 :
<? echo "PHP example with short-tags"; ?>
HTML Script Tags
HTML script tags look like this :
<script language="php"> echo "This is HTML script tags."; </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 :
<% echo 'This is ASP like style'; %>
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
Valid Codes
<?php echo 'This is a test string'; ?>
In the above example both semicolon(;) and a closing PHP tag are present.
<?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.
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
<?php echo("We are learning PHP case sensitivity <br />"); ECHO("We are learning PHP case sensitivity <br />"); EcHo("We are learning PHP case sensitivity <br />"); ?>
Output :
We are learning PHP case sensitivity
We are learning PHP case sensitivity
We are learning PHP case sensitivity
View the example in the browser
Practice here online :
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
<?php $amount = 200; echo("The Amount is : $amount <br />"); echo("The Amount is : $AMOUNT <br />"); echo("The Amount is : $amoUNT <br />"); ?>
Output :
The Amount is : 200
The Amount is :
The Amount is :
View the example in the browser
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 :
<?php function student_info($student_name, $class, $roll_no) { echo "The Name of student is : $student_name <br />"; echo "His Class is : $class and Roll No. is $roll_no"; } student_info("David Rayy", "V", 12) ?>
Output :
The Name of student is : David Rayy
His Class is : V and Roll No. is 12
View the example in the browser
Practice here online :
Example : Advance whitespace insensitivity
<?php function student_info ( $student_name, $class, $roll_no ) { echo "The Name of student is : $student_name <br />"; echo "His Class is : $class and Roll No. is $roll_no"; } student_info( "David Rayy", "V", 12 ) ?>
Output :
The Name of student is : David Rayy
His Class is : V and Roll No. is 12
View the example in the browser
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.
<?php // single space between $xyz, =, 11, +, 12 $xyz = 11 + 12; echo $xyz.'<br />'; // tabs and spaces $xyz = 11 + 12; echo $xyz; ?>
Output :
23
23
View the example in the browser
Practice here online :
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 :
<?php echo "How to make single line comment."; # This is a single line comment. //This is another way of single line comment. ?>
Output :
How to make single line comment.
View the example in the browser
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 :
<?php echo "How to make multiline comments"; /* These are a multiline comments testing, and these lines will ignored at the time of execution */ ?>
Output :
How to make multiline comments
View the example in the browser
Practice here online :
Multiple lines comments can not be nested
<?php echo "How to make multiline comments"; /* This is a multiline comment testing, and these lines will ignored at the time of execution */ echo "Following multiline comments formats are wrong."; /* This is a multiline comment /* testing, and these lines will ignored */ at the time of execution */ ?>
First PHP Script
Here is the first PHP script which will display "Hello World..." in the web browser.
<?php echo "Hello World..."; ?>
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
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.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> PHP Page</title> </head> <body> <?php echo "Hello World..."; echo "Hello World..."; ?> </body> </html>
Practice here online :