A PHP statement tells PHP to perform an action. One of the most common PHP statements is the echo statement. Its purpose is to display output. For instance, take a look at the following echo statement:
echo “Hi”;
An echo statement says to output everything that is between the double quotes (“). So, this statement tells PHP to output the word Hi.
The echo statement is a simple statement. PHP simple statements end with a semicolon (;). PHP reads a simple statement until it encounters a semicolon (or the PHP closing tag, discussed later in this chapter). PHP ignores white space. It doesn’t care how many lines it reads. It doesn’t consider the content or the syntax of the statement. It just reads until it finds a semicolon and then interprets the entire content as a single statement.
Leaving out the semicolon is a common error, resulting in an error message that looks something like this:
Parse error: expecting `’,’’ or `’;’’ in file.php on line 6
Notice that the error message gives you the line number where it encountered problems. Usually, the error is that the semicolon was left off in the line before the indicated line. In this case, the semicolon is probably missing on line 5.
You may prefer to use an editor that displays line numbers. Debugging your PHP scripts is much easier this way. Otherwise, you need to count the lines from the top of the script to find the line containing the error. If your script contains six lines, counting them is no big deal. If your script contains 553 lines, however, this is less than fun. Some editors allow you to indicate a line number, and the editor takes you directly there.
As far as PHP is concerned, an entire script full of simple statements can be written in one long line, as long as the statements are separated by semicolons.
However, a human would have a tough time reading such a script. Therefore, you should put simple statements on separate lines.
Sometimes several statements are combined into a block, which is enclosed by curly braces ({}). Statements in a block execute together. A common use of a block is in a conditional statement where statements are executed only if certain conditions are met. For instance, you may want to include the following instructions:
if (time = midnight)
{
put on pajamas;
go to bed;
for sleep;
}
The statements are enclosed in curly braces to ensure they execute as a block. If it’s midnight, then all three actions within the block are performed. If the time is not midnight, none of the statements execute (no pajamas, no clean teeth; no going to bed).
PHP statements that use blocks, such as if statements, are called complex statements. PHP reads the entire complex statement, not stopping at the first semicolon 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 there is a semicolon before the ending brace. This semicolon is required, but no semicolon is required after the ending curly brace.
Notice that the statements inside the block are indented. Indenting is not necessary for PHP. Indenting is strictly for readability. You should indent the statements in a block so that people reading the script can tell more easily where a block begins and ends. One of the more common mistakes when writing scripts is to leave out a closing curly brace, particularly when writing blocks inside blocks inside blocks. Tracking down a missing brace is much easier when the blocks are indented.
0 comments:
Post a Comment