PHP Architecture Overview

The job of a web server (such as Apache or Microsoft's IIS) is to reply to requests coming from a client (usually a web browser). When a browser connects to a web server, it requests information by sending a URL (Uniform Resource Locator). For example, if you browse to the URL, your web browser connects to the server at and requests a file named software.html.

After the web server has received this request, it must decide how to reply. If the requested file cannot be found, you'll see the all too familiar HTTP 404 - File not found. Most web servers will choose a response based on the extension of the requested file. A filename ending with .html (or .htm) is usually associated with a text file containing a HTML document.

Occasionally, you'll see a URL that ends in the suffix .php. A .php file is a script that is executed by a PHP processor embedded within the web server. The script is executed each time a client requests it. The web browser never sees the .php script; only the web server sees it. As the .php script executes, it sends information back to the browser (usually in the form of an HTML document).

Listing 15.1 shows a simple PHP script.

Listing 15.1 Simple.php

1  <?php

2    # Filename: Simple.php

3    echo "Hey there, I'm a PHP script!";

4  ?>

When you run this script (I'll show you how in a moment), the PHP interpreter will send the string "Hey there, I'm a PHP script!" to the browser.

PHP syntax might look a little strange at first, so here's a quick explanation. The script starts with the characters <?php: This tells the web server that everything that follows, up to the next ?>, is a PHP script and should be interpreted by the PHP processor. The next line is treated as a comment because it starts with a # character (PHP understands other comment characters, such as "//" as well). The third line is where stuff happens?this is a call to PHP's echo() function. echo() is pretty easy to understand; it just echoes a string to the web server. The characters on line 4 (?>) mark the end of the script.

Web browsers don't understand how to interpret PHP scripts; they prefer HTML documents. If you can use PHP to send textual data from the server to the browser, you can also send HTML documents (because an HTML document is textual data). This next PHP script (see Listing 15.2) will create an HTML document (and send it to the browser) as it executes.

Listing 15.2 SimpleHTML.php

 1 <?php

 2   # Filename: SimpleHTML.php

 3   echo "<HTML>\n";

 4   echo   "<HEAD>\n";

 5   echo     "<TITLE>SimpleHTML</TITLE>\n";

 6   echo   "<BODY>\n";

 7   echo     "<CENTER>I'm another simple PHP script</CENTER>\n";

 8   echo   "</BODY>\n";

 9   echo "</HTML>";

10 ?>

When you use a web browser to request this file (SimpleHTML.php), the server will execute the script and send the following text to the browser:





<CENTER>I'm another simple PHP script</CENTER>



The web browser interprets this as an HTML document and displays the result, as shown in Figure 15.1.

Figure 15.1. SimpleHTML.php in a browser.


Of course, if you want to display static HTML pages, PHP doesn't really offer any advantages?we could have produced this HTML document without PHP's help. The power behind a PHP script is that it can produce different results each time it is executed. Listing 15.3 shows a script that displays the current time (in the server's time zone).

Listing 15.3 Time.php

 1 <?php

 2   //Filename: Time.php


 4   $datetime = date( "Y-m-d H:i:s (T)" );


 6   echo "<HTML>\n";

 7   echo   "<HEAD>\n";

 8   echo     "<TITLE>Time</TITLE>\n";

 9   echo   "<BODY>\n";

10   echo     "<CENTER>";

11   echo       "The current time " . $datetime;

12   echo     "</CENTER>\n";

13   echo   "</BODY>\n";

14   echo "</HTML>";

15 ?>

Line 4 retrieves the current date and time, and assigns it to the variable $datetime. Line 11 appends the value of $datetime to a string literal and echoes the result to the browser. When you request this PHP script from within a browser, you see a result such as that shown in Figure 15.2.

Figure 15.2. Time.php in a browser.


If you request this document again (say by pressing the Refresh button), the web server will execute the script again and display a different result.

    Part II: Programming with PostgreSQL