Previous Section  < Day Day Up >  Next Section

1.4 Basic Rules of PHP Programs

This section lays out some ground rules about the structure of PHP programs. More foundational than the basics such as "how do I print something" or "how do I add two numbers", these proto-basics are the equivalent of someone telling you that you should read pages in this book from top to bottom and left to right, or that what's important on the page are the black squiggles, not the large white areas.

If you've had a little experience with PHP already or you're the kind of person that prefers playing with all the buttons on your new DVD player before going back and reading in the manual about how the buttons actually work, feel free to skip ahead to Chapter 2 now and flip back here later. If you forge ahead to write some PHP programs of your own, and they're behaving unexpectedly or the PHP interpreter complains of "parse errors" when it tries to run your program, revisit this section for a refresher.

1.4.1 Start and End Tags

Each of the examples you've already seen in this chapter uses <?php as the PHP start tag and ?> as the PHP end tag. The PHP interpreter ignores anything outside of those tags. Text before the start tag or after the end tag is printed with no interference from the PHP interpreter.

A PHP program can have multiple start and end tag pairs, as shown in Example 1-8.

Example 1-8. Multiple start and end tags
Five plus five is:

<?php print 5 + 5; ?>


Four plus four is:


 print 4 + 4;



<img src="vacation.jpg" alt="My Vacation">

The PHP source code inside each set of <?php ?> tags is processed by the PHP interpreter, and the rest of the page is printed as is. Example 1-8 prints:

Five plus five is:


Four plus four is:


<img src="vacation.jpg" alt="My Vacation">

Some older PHP programs use <? as a start tag instead of <?php. The <? is called the short open tag, since it's shorter than <?php. It's usually better to use the regular <?php open tag since it's guaranteed to work on any server running the PHP interpreter. The short tag can be turned on or off with a PHP configuration setting. Appendix A shows you how to modify your PHP configuration to control which open tags are valid in your programs.

The rest of the examples in this chapter all begin with the <?php start tag and end with ?>. In subsequent chapters, not all the examples have start and end tags ?but remember, your programs need them for the PHP interpreter to recognize your code.

1.4.2 Whitespace and Case-Sensitivity

Like all PHP programs, the examples in this section consist of a series of statements, each of which end with a semicolon. You can put multiple PHP statements on the same line of a program as long as they are separated with a semicolon. You can put as many blank lines between statements as you want. The PHP interpreter ignores them. The semicolon tells the interpreter that one statement is over and another is about to begin. No whitespace at all or lots and lots of whitespace between statements doesn't affect the program's execution. (Whitespace is programmer-speak for blank-looking characters such as space, tab, and newline.)

In practice, it's good style to put one statement on a line and to put blank lines between statements only when it improves the readability of your source code. The spacing in Examples Example 1-9 and Example 1-10 is bad. Instead, format your code as in Example 1-11.

Example 1-9. This PHP is too cramped
<?php print "Hello"; print " World!"; ?>

Example 1-10. This PHP is too sprawling

print "Hello"; 

print " World!"; 


Example 1-11. This PHP is just right

print "Hello";

print " World!";


In addition to ignoring whitespace between lines, the PHP interpreter also ignores whitespace between language keywords and values. You can have zero spaces, one space, or a hundred spaces between print and "Hello, World!" and again between "Hello, World!" and the semicolon at the end of the line.

Good coding style is to put one space between print and the value being printed and then to follow the value immediately with a semicolon. Example 1-12 shows three lines, one with too much spacing, one with too little, and one with just the right amount.

Example 1-12. Spacing

print        "Too many spaces"           ;

print"Too few spaces";

print "Just the right amount of spaces";


Language keywords (such as print) and function names (such as number_format) are not case-sensitive. The PHP interpreter doesn't care whether you use uppercase letters, lowercase letters, or both when you put these keywords and function names in your programs. The statements in Example 1-13 are identical from the interpreter's perspective.

Example 1-13. Keywords and function names are case-insensitive
// These four lines all do the same thing

print number_format(285266237);

PRINT Number_Format(285266237);

Print number_format(285266237);

pRiNt NUMBER_FORMAT(285266237);


As you've seen in some of the examples in this chapter, comments are a way to explain to other people how your program works. Comments in source code are an essential part of any program. When you're coding, what you are writing may seem crystal clear to you at the time. A few months later, however, when you need to go back and modify the program, your brilliant logic may not be so obvious. That's where comments come in. By explaining in plain language how the programs work, comments make programs much more understandable.

Comments are even more important when the person who needs to modify the program isn't the original author. Do yourself and anyone else who might have occasion to read your source code a favor and fill your programs with a lot of comments.

Perhaps because they're so important, PHP provides many ways to put comments in your programs. One syntax you've seen already is to begin a line with //. This tells the PHP interpreter to treat everything on that line as a comment. After the end of the line, the code is treated normally. This style of comment is also used in other programming languages such as C++, JavaScript, and Java. You can also put // on a line after a statement to have the remainder of the line treated as a comment. PHP also supports the Perl- and shell-style single-line comments. These are lines that begin with #. You can use # to start a comment in the same places that you can use //, but the modern style prefers // over #. Some single-line comments are shown in Example 1-14.

Example 1-14. Single-line comments with // or #
// This line is a comment

print "Smoked Fish Soup ";

print 'costs $3.25.';

# Add another dish to the menu

print 'Duck with Pea Shoots ';

print 'costs $9.50.';

// You can put // or # inside single-line comments

// Using // or # somewhere else on a line also starts a comment

print 'Shark Fin Soup'; // I hope it's good!

print 'costs $25.00!'; # This is getting expensive!

# Putting // or # inside a string doesn't start a comment

print '';

print '';

For a multiline comment, start the comment with /* and end with */. Everything between the /* and */ is treated as a comment by the PHP interpreter. Multiline comments are useful for temporarily turning off a small block of code. Example 1-15 shows some multiline comments.

Example 1-15. Multiline comments
/* We're going to add a few things to the menu: 

   - Smoked Fish Soup

   - Duck with Pea Shoots

   - Shark Fin Soup


print 'Smoked Fish Soup, Duck with Pea Shoots, Shark Fin Soup ';

print 'Cost: 3.25 + 9.50 + 25.00';

/* This is the old menu:

The following lines are inside this comment so they don't get executed.

print 'Hamburger, French Fries, Cola ';

print 'Cost: 0.99 + 1.25 + 1.50';


There is no strict rule in PHP about which comment style is the best. Multiline comments are often the easiest to use, especially when you want to comment out a block of code or write a few lines describing a function. However, when you want to tack on a short explanation to the end of a line, a //-style comment fits nicely. Use whichever comment style you feel most comfortable with.

    Previous Section  < Day Day Up >  Next Section