Zend PHP 7 Certification – PHP Basics – Syntax

This post covers the Syntax section of the PHP Basics chapter when studying for the Zend PHP 7 Certification.

When the PHP parser parses a file, it looks for opening and closing tags, which are <?php and ?> respectively.

PHP then will interpret code between them, which allows you to embed code in different documents, such as combining PHP with HTML code.

You can also use the PHP short open tag <?, which, prior to PHP 5.4. was discouraged as it required the short_open_tag PHP configuration directive was enabled.

However, as of PHP 5.4, the short tag is always available regardless of the short_open_tag PHP directive.

In files that only contain PHP, it is recommended to omit the PHP closing tag at the end of the file. This is to prevent any whitespace issues.

Please note that as of PHP 7.0, the ASP tags <%, %>, <%=, and the script tag <script language="php"> have been removed.

PHP requires instructions to be terminated with a semi-colon. 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.

<?php
    echo 'This is a test';
?>

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

<?php echo 'We omitted the last closing tag';

Comments

Single line comments in PHP only comment to the end of the line or the current block of PHP code, whichever comes first.

<?php
    // Here is a comment
?>

Multiple line comments start with /* and end with the first */ encountered.

<?php
/* Here is a comment
   spanning across
   multiple lines */

/*
   echo 'This is a test'; /* This comment will cause a problem */
*/

Please note that HTML comments have no meaning in the PHP parser. So the below code will still execute the someFunction() function.

<!-- comment
<?php echo someFunction(); ?>
-->

View the other sections:

Note: This article is based on PHP version 7.0.