1.3. PHP Syntax

The PHP section that you add to your HTML file consists of a series of PHP statements. Each PHP statement is an instruction to PHP to do something. PHP statements can be simple or complex.

1.3.1. Using simple statements

Simple statements are an instruction to PHP to do one simple action. The echo statement shown in Listing 1-2 is a simple PHP statement that instructs PHP to output the text between the double quotes. PHP simple statements follow these rules:

  • PHP statements end with a semicolon or the PHP ending tag. PHP doesn't notice white space or the end of lines. It continues reading a statement until it encounters a semicolon or the PHP closing tag, no matter how many lines the statement spans.

  • PHP statements may be written in either upper- or lowercase. In an echo statement, Echo, echo, ECHO, and eCHo are all the same to PHP.

The following example contains two echo statements that produce the same output:

echo "<p>Hello World</p>";
echo "<p>Hello
 World</p>";

PHP reads the second echo statement until it encounters the semicolon on the second line, so that both statements produce the following output:

<p>Hello World</p>

The following is another valid PHP statement that produces the same output:

<?php echo "<p>Hello World!</p>" ?>

The echo statement is on the same line as the PHP tags. PHP reads the statement until it reaches the closing tag, which PHP sees as the end of the statement. The next example also produces the same output:

<?php
    echo "<p>Hello</p>"; echo "<p>World</p>";
?>

This example contains two PHP echo statements on one line, both ending in a semicolon. If you wanted to, you could write the entire PHP section in one long line, as long as you separated statements with semicolons. However, a script written this way would be impossible for people to read.

1.3.2. Using complex statements

Sometimes groups of simple statements are combined into a block. A block is enclosed by curly braces, { and }. A block of statements execute together. A common use of a block is a conditional block, in which statements are executed only when certain conditions are true. For instance, you might want your script to do the following:

if (the sky is blue)
{
  put leash on dragon;
  take dragon for a walk in the park;
}

These statements are enclosed in curly braces to ensure that they execute as a block. If the sky is blue, both put leash on dragon and take dragon for a walk in the park are executed. If the sky is not blue, neither statement is executed (no leash; no walk), and you have an irritated dragon on your hands.

PHP statements that use blocks, such as if statements (which we explain in Chapter 2 in this minibook), are complex statements. PHP reads the entire complex statement, not stopping at the first semicolon that it encounters. PHP knows to expect one or more blocks and looks for the ending curly brace of the last block in complex statements. Notice that a semicolon appears before the ending brace. This semicolon is required, but no semicolon is required after the ending curly brace.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.136.226