Previously, you were reminded of how a Web server responds to a request for a static HTML file. The following steps describe the request-response sequence for a PHP file:
The Web browser requests a document with a .php extension (or any extension set to be treated as a PHP file).
The Web server sends the request on to the PHP parser, which is either built into the Web server binary or exists separately as a filter or CGI executable.
The PHP parser scans the requested file for PHP code.
When the PHP parser finds PHP code, it executes that code and places the resulting output (if any) into the place in the file formerly occupied by the code.
This new output file is sent back to the Web server.
The Web server sends the output file along to the Web browser.
The Web browser displays the output.
To combine PHP code with HTML, the PHP code must be escaped, or set apart, from the HTML. The following method is the default configuration of the PHP engine:
<?php // PHP code goes here. ?>
The PHP engine will consider anything within the <?php opening tag and the ?> closing tag as PHP code. You can also escape your PHP code by using the <? opening tag and the ?> closing tag, or by using the <SCRIPT Language=php> opening tag and the </SCRIPT> closing tag.
Now it's time to write that first script. Your first PHP script will display "Hello World! I'm using PHP!" in the browser window.
First, open your favorite text editor and create a simple text file called first.php. In this text file, type the following code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>My First PHP Script</TITLE> </HEAD> <BODY> <?php echo "<P>Hello World! I'm using PHP!</P>\n"; ?> </BODY> </HTML>
Save this file and place it in the document root of your Web server. Now access it with your browser at its URL, http://127.0.0.1/first.php. In your browser window, you should see this:
Hello World! I'm using PHP!
If your server has an actual machine and domain name, such as www.yourcompany.com, feel free to use it in place of 127.0.0.1 (which is the default localhost).
If you use your browser to view the source of the document, you should just see this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>My First PHP Script</TITLE> </HEAD> <BODY> <P>Hello World! I'm using PHP!</P> </BODY> </HTML>
As the PHP code was rendered by the PHP parser, all that remains visible is the HTML output.
Now, take a look at the PHP code used in the script. It contains three elements: the command (echo), the string (<P>Hello World...), and the instruction terminator (;).
Familiarize yourself now with echo, because it will likely be your most often-used command. The echo() function is used to output information-in this case, to print <P>Hello World! I'm using PHP!</P> in the HTML file. The instruction terminator is such an important concept that it warrants its own section.
The instruction terminator, also known as the semicolon, is absolutely required. If you do not end your command with a semicolon, the PHP engine will not parse your PHP code properly, and ugly errors will occur. For example, this code:
<?php echo "<P>Hello World! I'm using PHP!</P>\n" echo "<P>This is another message.</P>"; ?>
produces this nasty error:
Parse error: parse error, expecting "," or ";" in /path/to/your/file/filename.php on line 9
Avoid this error at all costs-remember to terminate commands with a semicolon!
Right up there with remembering to end your commands with semicolons is remembering to escape elements like quotation marks. When you use quotation marks inside other quotation marks, the inner pairs must be delineated from the outside pair using the escape (\) character (also known as a backslash). For example, the following code will produce another parse error, because the term "cool" is surrounded by double quotes, within a double-quoted string:
<?php echo "<P>I think this is really "cool"!</P>"; ?>
This code should instead look like this:
<?php echo "<P>I think this is really \"cool\"!</P>"; ?>
Now that the inner quotation marks are escaped, the PHP parser will skip right over them because it knows that these characters should just be printed and that they have no other meaning. The same concept holds true for single-quoted elements within other single-quoted strings-escape the inner element. Single-quoted strings within double-quoted strings, and vice versa, require no escaping of characters.
Whether you're adding comments to static HTML documents or to PHP scripts, code-commenting is a good habit to cultivate. Comments will help you, and others who might have to edit your documents later, get a handle on what's going on in your documents.
HTML comments are ignored by the browser and are contained within <!-- and --> tags. For example, the following comment reminds you that the code following it contains your logo graphic:
<!-- logo graphic goes here -->
Similarly, PHP comments are ignored by the parsing engine. PHP comments are usually preceded by double slashes, like this:
// this is a comment in PHP code
Other types of commenting can be used in PHP files, as in the following:
# This is shell-style style comment
/* This begins a C-style comment that runs onto two lines */
HTML and PHP comments are used extensively throughout this book to explain blocks of code. Get used to reading comments, and try to pick up the habit of using them. Writing clean, bug-free code, with plenty of comments and white space, will make you popular among your developer peers because they won't have to work extra hard to figure out what your code is trying to do!
Now that you have a handle on how PHP documents are created and used, the next section will introduce you to the PHP variables and operators that will become integral parts of your scripts.