PHP Syntax and Tags
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
- Omit the PHP closing tag at the end of the file
Default Syntax
The default syntax starts with «».
Short open Tags
The short tags starts with «». Short style tags are only available when they are enabled in php.ini configuration file on servers.
Omit the PHP closing tag at the end of the file
It is recommended that a closing PHP tag shall be omitted in a file containing only PHP code so that occurrences of accidental whitespace or new lines being added after the PHP closing tag, which may start output buffering causing uncalled for effects can be avoided.
PHP Statement separation
In PHP, statements are terminated by a 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
In the above example, both semicolon(;) and a closing PHP tag are present.
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.
"); ECHO("We are learning PHP case sensitivity
"); EcHo("We are learning PHP case sensitivity
"); ?>
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.
"); echo("The Amount is : $AMOUNT
"); echo("The Amount is : $amoUNT
"); ?>
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 :
"; echo "His Class is : $class and Roll No. is $roll_no"; > student_info("David Rayy", "V", 12) ?>
The Name of student is : David Rayy His Class is : V and Roll No. is 12
Example: Advance whitespace insensitivity
"; echo "His Class is : $class and Roll No. is $roll_no"; > student_info( "David Rayy", "V", 12 ) ?>
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, $xyz returns the same value.
'; // tabs and spaces $xyz = 11 + 12; echo $xyz; ?>
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.
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 be ignored
at the time of execution */
How to make multiline comments
Multiple lines comments can not be nested
First PHP Script
Here is the first PHP script which will display «Hello World. » in the web browser.
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 is sent directly 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 an opening tag () of PHP and ignore the rest parts of the document.
PHP and HTML are seen together in the following example.
Practice here online :
Previous: Install WAMP
Next: PHP Variables
Follow us on Facebook and Twitter for latest update.
PHP: Tips of the Day
How to Sort Multi-dimensional Array by Value?
Try a usort, If you are still on PHP 5.2 or earlier, you’ll have to define a sorting function first:
function sortByOrder($a, $b) < return $a['order'] - $b['order']; >usort($myArray, 'sortByOrder');
Starting in PHP 5.3, you can use an anonymous function:
And finally with PHP 7 you can use the spaceship operator:
usort($myArray, function($a, $b) < return $a['order'] $b['order']; >);
To extend this to multi-dimensional sorting, reference the second/third sorting elements if the first is zero — best explained below. You can also use this for sorting on sub-elements.
usort($myArray, function($a, $b) < $retval = $a['order'] $b['order']; if ($retval == 0) < $retval = $a['suborder'] $b['suborder']; if ($retval == 0) < $retval = $a['details']['subsuborder'] $b['details']['subsuborder']; > > return $retval; >);
If you need to retain key associations, use uasort() — see comparison of array sorting functions in the manual
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook
Instruction separation
As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement. The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block. The closing tag for the block will include the immediately trailing newline if one is present.
Example #1 Example showing the closing tag encompassing the trailing newline
echo "Some text"; ?> No newline "But newline now" ?>
The above example will output:
Some textNo newline But newline now
Examples of entering and exiting the PHP parser:
echo 'This is a test'; ?> echo 'This is a test' ?> echo 'We omitted the last closing tag';
Note:
The closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using include or require, so unwanted whitespace will not occur at the end of files, and you will still be able to add headers to the response later. It is also handy if you use output buffering, and would not like to see added unwanted whitespace at the end of the parts generated by the included files.
PHP 8.2
Everything outside of a pair opening and closing tags is ignored by the PHP parser which allows files to have mixed content.
When PHP parses file, it looks for opening and closing tags, which are tell to start stop interpreting the code between them.