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>"; ...

Get PHP & MySQL® Web Development All-in-One Desk Reference for Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.