Saturday, December 8, 2007

Statements

Statements

Within the PHP delimiters, you can write any number of statements. There are two types of statement: single line, and multi-line.

Single line statements must end with a semi-colon, unless they are the last statement before the closing delimiter of a PHP block, when the semi-colon can be omitted. So, the following are both legal:

    two plus two is 

two plus two is

Single line statements may also contain, paradoxically, line breaks. This is because PHP doesn't assume a statement is finished until it encounters a semi-colon, and line breaks are just considered as whitespace, which, in most places within PHP delimiters, PHP ignores. Similarly, there is no requirement that a semi-colon be followed by a line break before the next statement begins. So, the following two sets of statements are exactly equivalent:

    

Multi-line statements make use of a construct called a code block. A code block contains multiple PHP statements, enclosed in curly brackets ({ }):

    {
echo("hello");
echo(2 + 2);
}

Although you can use code blocks just like this in your PHP code, they don't have any effect on the code (in particular, unlike in C or Java, they have no effect on variable scope). Code blocks are only of use when used in conjunction with control structures, such as loops, or if statements:

    if (3 > 2) {
echo("hello");
echo(2 + 2);
}

In this case, we have a multi-line statement. The statement consists of the control structure and the code block. Since it is clear where the statement ends, there is no semi-colon on the end of the statement – only on the end of the statements contained within the code block.

Note that it is also possible to ‘un-escape’ from PHP within a code block (but not within a single line statement), provided that you subsequently escape back to PHP and finish off the block, as follows:

     2) {
echo("hello");
?>
This line is not interpreted as PHP code, and is only printed out if the code
block is executed.


Comments

PHP provides several methods for including comments in code. The simplest is to insert a double slash, which tells PHP to ignore everything up to the next line break:

   

You can do the same with a hash sign:

   

Of course, you can also insert comments in other places – not just after the end of a statement. You can add comments anywhere where PHP lets you add whitespace. The following are all legal:

   

PHP also supports C++/Java style multi-line comments, using the /* */ delimiters:

    $Calculation = (($x/$y) * 7.5) / $z ;
/* The above calculation takes the price
differential, x and divides it by the
number of customers, y. We multiply this
figure by the current interest rate (7.5)
and then divide it by the decimal figure
stored in z */

It's worth remembering that these styles of comment will only work within the PHP code delimiters. If PHP encounters these markers outside the delimiters, they'll be treated just like any other text, and will be included in the output. You can use this to your advantage when using PHP for web scripting, by including HTML comments in the output, like this:




// this PHP comment will show up in the browser - oops


This can be useful for debugging or for making generated HTML source code more understandable.

No comments: