This section lays out some ground rules about the structure of PHP programs. More foundational than 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 Blu-Ray 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 behave unexpectedly, or the PHP engine complains of “parse errors” when it tries to run your program, revisit this section for a refresher.
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 engine ignores anything outside of those tags. Text before the start tag or after the end tag is printed with no interference from the PHP engine. You can leave off the end tag at the end of a PHP file. If the PHP engine reaches the end of a file and doesn’t see a PHP end tag, it acts as if there was one as the very last thing in the file. This is very useful for ensuring that invisible extra stuff (such as blank lines) after an end tag doesn’t accidentally make it into your program output.
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 engine, 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 engine. Support for 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 in order for the PHP engine to recognize your code.
2. Whitespace and Case-Sensitivity
Like all PHP programs, the examples in this section consist of a series of statements, each of which ends 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 engine ignores them. The semicolon tells the engine 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 spaces, tabs, and newlines.)
In practice, it’s good style to put one statement on a line and blank lines between statements only when it improves the readability of your source code. The spacing in Examples 1-9 and 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 “World!”; ?>
Example 1-11. This PHP is just right
print “Hello”; print “World!”;
In addition to ignoring whitespace between lines, the PHP engine 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 engine 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 engine’s perspective.
Example 1-13. Keywords and function names are case-insensitive
// These four lines all do the same thing
print number_format( 20853904);
PRINT Number_Format( 20853904 );
Print number_format( 20853904);
pRiNt NUMBER_FORMAT( 20853904);
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.
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
For a multiline comment, start the comment with /* and end it with */. Everything between the /* and */ is treated as a comment by the PHP engine. 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.
Source: Sklar David (2016), Learning PHP: A Gentle Introduction to the Web’s Most Popular Language, O’Reilly Media; 1st edition.