Understanding How PHP Is Parsed

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:

  1. The Web browser requests a document with a .php extension (or any extension set to be treated as a PHP file).

  2. 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.

  3. The PHP parser scans the requested file for PHP code.

  4. 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.

  5. This new output file is sent back to the Web server.

  6. The Web server sends the output file along to the Web browser.

  7. The Web browser displays the output.

Because the PHP code is parsed by the server, this method of code execution is called server-side. When code is executed by the browser, such as with JavaScript, it is called client-side.

Code Cohabitation and PHP Tags

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, 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 (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 Importance of the Instruction Terminator

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!

Escaping Your Code

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.

Commenting Your Code

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.

PHP Essentials
PHP Essentials, 2nd Edition
ISBN: 1931841349
EAN: 2147483647
Year: 2002
Pages: 74

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net