PHP Syntax
From TPC! Web Development
Contents |
Introduction
PHP's syntax is based on several popular programming languages. The most influence stems from the C language; however, Perl and Java also have had an impact on the syntax of PHP. The synergism created by combining different elements from each language has helped keep PHP syntax simple and easy to understand.
Four Types of PHP Tags
There are four types of PHP tags that can be used to insert code. Code is always inserted between a set of one of the four listed PHP tags. These tags tell PHP to execute the code entered between the tag. The four tags available include:
<?php // Standard Tags ?> <? // Short Tags ?> <script language="php"> // Script Tags </script> <% // ASP Tags %>
Which PHP tag-type should I use?
The best PHP tags to use are standard tags. Using standard tags guarantees backwards compatibility (use with older versions of PHP) and portability. Standard tags cannot be disabled with the PHP configuration file. The other tag types can be disabled from within the PHP configuration file.
Output Variable Using Short Tags: <?= $variable ?> Output Variable Using Standard Tags: <?php echo $variable; ?>
Short tags are great to use due to their ease of use. <?= $variable ?> is much easier than typing <?php echo $variable; ?>. The disadvantages to using short tags include conflicts with XML headers, and the possibility of these being disabled in the PHP configuration file.
Script tags and ASP tags aren't used often. Script tags were implemented for HTML editors that couldn't handle PHP tags. ASP tags are turned off by default, but can be turned on in the PHP configuration file.
PEAR Compliance
PEAR states that <?php ?> must always be used to delimit PHP code, not the <? ?> shorthand method. This is required to be PEAR-compliant and is the most portable way to use PHP code on different operating systems and setups.
The Semicolon
Most instructions given in a PHP script require ending with a semicolon. Examples of instructions that do not require a semicolon include for and while loops, and if/else statements that use brackets and not shorthand syntax. Although the last line before a closing tag does not technically require a semicolon, each line should still be terminated with a semicolon:
<?php execute_function(); $variable = 'value'; ?>
PHP Comments
A crucial part of programming is comments. Without comments to explain what functions, classes, properties, etc., do, a programmer could easily get lost when coming back to any given part of even a small project. A lot of projects suffer when a developer has to relearn what he/she previously did or learn what another developer did without comments. The different types of PHP comments are:
<?php // Single line comment # Single line comment /* Multi-line comment */ /** * Example of documentation used in APIs * * @access public * @param string $string * @param int $integer * @return unknown */ function example_function($string, $integer) { /* ... code ... */ } ?>
Single line comments are ended quite literally by pressing ENTER and moving to a new line. The ENTER key creates a newline character (\r, \n or \r\n), which signifies the end and start of a new line. The closing PHP tag—<?—will also end a single line comment and that specific block of PHP code.
Code Blocks
A block of code is a series of statements enclosed between two braces:
{ // Execute a function f(); // Function to execute }
Code blocks are used to enclose functions, classes, if/else statements, for loops, while loops, and more. Code blocks can also be nested, meaning you can have code blocks within other code blocks. Certain rules apply to functions and classes; however, those instances will not be covered in this section.

