1.4. Writing PHP Code

PHP code must be read by humans, as well as by the PHP software. PHP scripts are written by humans and must be modified, updated, and maintained by humans. The script might need to be modified a year or two in the future when the original programmer has moved on to retirement on a tropical beach. The person who must modify the script needs to be able to read and understand the script, which he or she has never seen before. Consequently, the PHP code must be written in a style that's easy for humans to comprehend quickly.

In general, each PHP simple statement is written on a single line ending with a semicolon. An exception is echo statements that echo long text strings. It's not necessary to end the statement at the end of a line and start a new statement on the next line. You can write the statement over several lines, as long as you're careful to end the statement with the semicolon on the last line. It's also a good idea to indent the lines after the first line to show clearly that the additional lines are part of the same statement, as follows:

echo "This is a really,
  really, really, really
  long statement";

When writing blocks of statements, coding style dictates that you should indent the block statements to clearly show where the block begins and ends. For instance, in the following example of a conditional statement, the simple statements in the block are indented:

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

PHP ...

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.