PHP Syntax - dummies

By Steve Suehring, Janet Valade

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 divided into simple or complex statements.

The PHP language syntax is similar to the syntax of C, so if you have experience with C, you’ll be comfortable with PHP. PHP is actually simpler than C because it doesn’t include some of the more difficult concepts of C — concepts not required to program websites.

Simple PHP statements

Simple statements are an instruction to PHP to do one simple action. The echo statement 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. But variable names are case sensitive, just like in JavaScript.

The following is a valid PHP statement:

<?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:

    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 difficult for people to read.

Complex PHP 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, 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.