Zend PHP 5 – Basics – Syntax

This article covers the “Syntax” section in the “Basics” chapter of the Zend PHP 5 Certification.

There are currently four ways constructing PHP code. These are:

<?php 
    // Some stuff
?>
<? 
    // Some stuff
?>
<script language='php'>
    // Some stuff
</script>
<% 
    // Some stuff
%>

The first option is the most common construction and the second option is just a shorter version of the first, known as “short tags”.

As of PHP 7, the <script language=’php’> and ASP style tags are being removed. Most server have the “asp_tags” directive configuration turned off, so if these tags need to be used then this directive should be enabled.

Should you use short tags? There are arguments both for and against using them including conflicts with XML tags. As of PHP 5.4 short tags are fully supported, and most servers will be compatible with short tags. However if your code needs to support PHP versions earlier than 5.4, then it is advised to use the longer version of the tags.

If the files contains pure PHP code, it is preferred to omit the closing PHP tag. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects.

PHP code statements should be terminated by using a semi-colon. For example:

<?php 
    $foo = "Test";
    echo $foo;
?>

Single line comments in PHP are used like so:

<?php
    // Here is a comment
?>

And a multiple line comment is used as per the below:

<?php
    /* Here is a comment
       commenting on some
       various things */
?>

Do numerous comments affect performance? The interpreter still has to process lines to work out if they are comments, but the time required for this is negligible. If your website or system is slow, then comments will not be the reason.

Note: This article is based on PHP version 5.5.