Web Scripting with PHP

PHP has emerged as a component of many medium- and large-scale web database applications. This isn't to say that other scripting languages don't have excellent features. However, there are many reasons that make PHP a good choice, including:

  • PHP is open source, meaning it is entirely free. As such, community efforts to maintain and improve it are unconstrained by commercial imperatives.

  • One or more PHP scripts can be embedded into static HTML files and this makes client-tier integration easy. On the down side, this can blend the scripts with the presentation; however the template techniques described in Chapter 13 can solve most of these problems.

  • There are over 15 libraries for native, fast access to the database tier.

  • Fast execution of scripts. With the new innovations in the Zend engine for script processing, execution is fast, and all components run within the main memory space of PHP (in contrast to other scripting frameworks, in which components are in distinct modules). Empirical evidence suggests that for tasks of at least moderate complexity, PHP is faster than other popular scripting tools.

  • Platform and operating-system flexibility. Apache runs on many different platforms and under selected operating systems; PHP runs on all these and more when integrated with other web servers.

  • PHP is suited to complex systems development. It is a fully featured programming language, with more than 50 function libraries.

PHP4 represents a complete rewrite of the underlying scripting engine used in PHP3. The significant difference is a change in the model used to run scripts with the scripting engine. The PHP3 scripting engine was an interpreter. Each line of code in a script was read, parsed, and executed. If a statement in the body of a loop is executed 100 times, the line of code is reinterpreted 100 times using PHP3. This model is slow for complex scripts, but fast for short scripts.

The PHP4 script-processing model is different and designed for larger applications. A script is read, parsed, and compiled into an intermediate format, and then the intermediate code is executed by the PHP4 Zend engine script executor. This means that each line in the script is interpreted from its raw form only once, even if it is executed hundreds of times. Moreover, compilation allows optimization of code segments. The result is a performance improvement in PHP4 for all but very simple scripts.

The architecture of the PHP4 scripting environment is shown in Figure 1-3 (image from Zend Technologies Inc.). As shown, PHP4 is a module of the web server software. The PHP software itself is divided into two components: the function libraries or modules, and the Zend engine.

Figure 1-3. The architecture of the PHP4 scripting environment

When a user agent makes a request to the web server for a PHP script, six steps occur:

  1. The web server passes the request to the Zend engine's web server interface.

  2. The web server interface calls the Zend engine and passes parameters to the engine.

  3. The PHP script is retrieved from disk by the engine.

  4. The script is compiled by the runtime compiler.

  5. The compiled code is run by the engine's executor and may include calls to function modules. The output of the executor is returned to the web server interface.

  6. The web server interface returns output to the web server (which, in turn, returns the output as an HTTP response to the user agent).

How the PHP scripting engine is managed and run depends on how the PHP module is included in the Apache web server installation process. In the instructions provided in Appendix A, the PHP module library is statically linked with the Apache httpd binary executable. This means that the PHP scripting engine is loaded into main memory when Apache runs, making the PHP engine run faster. The drawbacks are that Apache with a static PHP library consumes more memory than if the module is loaded dynamically, and that the module upgrade process is less flexible.