Skip to main content

PHP Basic Syntax


PHP tags 

When PHP parses a file, it looks for opening and closing tags, which are  and ?> which tell PHP to start and stop interpreting the code between them. Parsing in this manner allows PHP to be embedded in all sorts of different documents, as everything outside of a pair of opening and closing tags is ignored by the PHP parser.
PHP also allows for short open tag  (which is discouraged since it is only available if enabled using theshort_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tagsoption).
If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.
echo "Hello world";
// ... more code
echo "Last statement";
// the script ends here with no PHP closing tag

Escaping from HTML 

Everything outside of a pair of opening and closing tags is ignored by the PHP parser which allows PHP files to have mixed content. This allows PHP to be embedded in HTML documents, for example to create templates.
This is going to be ignored by PHP and displayed by the browser.

echo 'While this is going to be parsed.'?>This will also be ignored by PHP and displayed by the browser.
This works as expected, because when the PHP interpreter hits the ?> closing tags, it simply starts outputting whatever it finds (except for an immediately following newline - see instruction separation) until it hits another opening tag unless in the middle of a conditional statement in which case the interpreter will determine the outcome of the conditional before making a decision of what to skip over. See the next example.
Using structures with conditions
Example #1 Advanced escaping using conditions
if ($expression == true): ?>  This will show if the expression is true.
else: ?>  Otherwise this will show.
endif; ?>
In this example PHP will skip the blocks where the condition is not met, even though they are outside of the PHP open/close tags; PHP skips them according to the condition since the PHP interpreter will jump over blocks contained within a condition that is not met.
For outputting large blocks of text, dropping out of PHP parsing mode is generally more efficient than sending all of the text through echo or print.
In PHP 5, there are up to five different pairs of opening and closing tags available in PHP, depending on how PHP is configured. Two of these,  and , are always available. There is also the short echo tag , which is always available in PHP 5.4.0 and later.
The other two are short tags and ASP style tags. As such, while some people find short tags and ASP style tags convenient, they are less portable, and generally not recommended.
Note:
Also note that if you are embedding PHP within XML or XHTML you will need to use the tags to remain compliant with standards.
PHP 7 removes support for ASP tags and 
    This syntax is removed in PHP 7.0.0.

5.  <% echo 'You may optionally use ASP-style tags'; %>
    Code within these tags <%= $variable; %> is a shortcut for this code <% echo $variable; %>
    Both of these syntaxes are removed in PHP 7.0.0.
Short tags (example three) are only available when they are enabled via the short_open_tag php.iniconfiguration file directive, or if PHP was configured with the --enable-short-tags option.
ASP style tags (example five) are only available when they are enabled via the asp_tags php.ini configuration file directive, and have been removed in PHP 7.0.0.

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.
    echo 'This is a test';?>
echo 'This is a test' ?>
echo 'We omitted the last closing tag';

Comments 

PHP supports 'C', 'C++' and Unix shell-style (Perl style) comments. For example:
    echo 'This is a test'// This is a one-line c++ style comment
    /* This is a multi line comment
       yet another line of comment */
    
echo 'This is yet another test';
    echo 
'One Final Test'# This is a one-line shell-style comment?>
The "one-line" comment styles only comment to the end of the line or the current block of PHP code, whichever comes first. This means that HTML code after // ... ?> or # ... ?> WILL be printed: ?> breaks out of PHP mode and returns to HTML mode, and // or # cannot influence that. If the asp_tags configuration directive is enabled, it behaves the same with // %> and # %>. However, the 
 tag doesn't break out of PHP mode in a one-line comment.

This is an # echo 'simple';?> example


The header above will say 'This is an  example'.

http://php.net/manual/en/language.basic-syntax.php

Comments