An Overview of PHP

PHP is a widely used, server-side, cross-platform scripting language that is freely available (including the source code) under the PHP License. The initials come from the earliest version of the language developed by Rasmus Lerdorf (a Danish-Greenlandic programmer) in 1995 which he called "Personal Home Page Tools". He later rewrote PHP using C, and extended the functionality to include the ability to communicate with a database. PHP now had the potential to be used to create web applications, and entered the public domain in June 1995 when Lerdorf released it (as version 2), with the intention of accelerating its development. Version 2 already had most of the features familiar to today’s PHP users, including the ability to process form data and to embed HTML within PHP source code.

Two Israeli developers at the Technion IIT, Zeev Suraski and Andi Gutmans, rewrote the parser in 1997, leading to the release of version 3 (somewhat recursively renamed to PHP: Hypertext Preprocessor) in 1998. Suraski and Gutmans then rewrote PHP's core, producing the Zend Engine in 1999 which was introduced in version 4 of PHP, released in 2000. They also founded Zend Technologies, which has its headquarters in California and a technology centre in Israel. PHP 5 was released in 2004, powered by the new Zend Engine II, and included new features such as improved support for object-oriented programming, and various performance enhancements. PHP is now developed and maintained by the PHP Group as a de facto standard. The current release (as of February 2009) is version 5.2.9.

Lerdorf initially intended to use PHP to replace a set of Perl scripts he was using to maintain his personal home page, on which he displayed his CV and other information, and kept a record of the number of people visiting his site. By 1999, Suraski and Gutmans had moved the technology forward, creating the Zend engine that powered PHP version 4, released in 2000. Version 5 emerged in 2004, powered by version II of the Zend Engine, and is the only stable version still being actively maintained. PHP 6 is currently in development, and will address some of the security issues identified in previous versions, as well as adding support for Unicode. PHP is used to create dynamic web pages and web applications, and can be deployed on virtually all server platforms. It is estimated that currently (2009) PHP is used in some twenty million or so web domains, and is installed on more than a million web servers.

PHP scripts are embedded within a Web page using the opening and closing delimiters <?php and ?>. Only code that appears within these delimiters is parsed by the PHP interpreter. Any other code (chiefly HTML) is sent to the client browser unchanged by the server. A Web page that includes PHP scripting is typically given a file extension of .php. Many of the functions provided by the library files distributed with the PHP core distribution are similar to those found in the C programming language, as are the control structures used (for example, conditional statements and loops). PHP developers may write their own extensions in C to add functionality to the language which can either be compiled into PHP or loaded dynamically at runtime. The PHP Extension Community Library (PECL) provides a repository for such extensions to the PHP language. As of version 4, the PHP parser compiles input into bytecode to be processed by the Zend Engine, resulting in faster execution. Like Perl, PHP is one of the three Ps associated with the LAMP solution stack for web development.

The simple PHP code below checks which browser the client computer is using. The user agent string that the browser sends as part of its request is stored in the variable $user_agent, and is used to determine whether the client browser is Opera, Firefox or Internet Explorer (or none of these).

<?php
  $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
  if(strpos($user_agent, 'Opera') !== false)
  {
    echo ' You are using Opera';
  }
    elseif(strpos($user_agent, 'Gecko') !== false)
  {
    echo ' You are using Firefox';
  }
  elseif(strpos($user_agent, 'MSIE') !== false)
  {
    echo 'You are using Internet Explorer';
  }
  else
  {
    echo 'You are using a browser other than Opera, Internet Explorer or Firefox.';
  }
?>

You will see from the above how the PHP delimiters (<?php and ?>) are used to enclose the PHP code itself. In a typical web page that uses PHP, these delimiters separate the PHP code from the HTML code. PHP variable names are case-sensitive, and variables are prefixed with a dollar sign ($). The variable types do not have to be specified in advance. PHP acts like a freeform language in that it treats a newline as white space (except when the newline occurs inside a quoted string). Program statements are terminated with a semicolon.

The echo statement has been used in the example above to output text (for example, in a web browser window). The syntax and structure of PHP code is similar in many respects to that used in C programming. Conditional statements, loop constructs and the way in which function calls are made will all be familiar to those that have programmed in C. Many of the library functions that accompany PHP are similar to those found in the common C libraries such as stdio. User-defined functions can be created without being prototyped, and functions can be defined inside code blocks. All function calls must include parentheses, with the exception of class constructor functions called with the PHP new operator (where their use is optional). Single line comments in PHP can be written using either a hash (#) symbol, or two forward slash symbols (//). Block comments are enclosed between pairs of forward slash and asterisk symbols. Examples of the three methods of commenting code are shown.

# This is a PHP comment on one line.

// This is another PHP comment on one line.

/* This is a PHP block comment. Block comments are
useful for providing a more comprehensive explanation
of what is happening in a particular section of code. */

Data types include signed integers, floating point numbers, and boolean values, although the range of values that each type can take are platform-dependent. Integer values can be expressed as octal or hexadecimal numbers as well as decimal, and floating point types can be expressed using scientific notation. Where a boolean value is expected, a value of zero is interpreted as false, while any non-zero value will be interpreted as true. The NULL keyword is provided to represent a constant (null) value that may be assigned to a variable that does not have a specific value. PHP arrays can contain elements of any valid PHP data type, object, or even other arrays. A PHP also supports strings. A string in PHP is a series of characters enclosed within single or double quotes.

PHP is frequently used with the MySQL database server because of the ease with which these two technologies can be used together to create web applications. The PHP pre-processor is implemented as a software module that is integrated into the web server application (for example, mod_php in the Apache web server. When a web document containing PHP code is requested by a client, the code is sent to the pre-processor to be compiled and executed. The resulting output (in the form of HTML code) is returned to the server, which forwards it unaltered to the client. The simple example web page below can be used to illustrate how this works:

<html>
<head>
<title>A PHP Example</title>
</head>
<body>
<?php
  echo '<p>This line of HTML was generated by a PHP script embedded into an HTML document</p>';
?>
</body>
</html>

The server passes the PHP script to the PHP pre-processor module, which carries out the necessary processing and returns the resulting HTML code to the server. The file sent to the client browser will be as follows:

<html>
<head>
<title>A PHP Example</title>
</head>
<body>
<p>This line of HTML was generated by a PHP script embedded into an HTML document</p>
</body>
</html>

From version 4 onwards, PHP code is compiled at runtime into bytecode that can be processed by the Zend Engine. This allows for much faster execution than for previous versions, in which the source code was interpreted. Execution speed can be increased by pre-compiling the source code using a PHP compiler, which may include code optimisation features that reduce execution time still further. The improved support for object-oriented programming in PHP 5 included extending the visibility options for properties or methods defined in a class. Visibility is defined by prefixing the declaration with the keywords public, protected or private. Properties or methods declared as public can be accessed from anywhere. Those declared as protected can only be accessed by parent or inherited classes (and the class within which the declaration occurs, obviously). Those declared as private may only be accessed by the class that defines them. Version 5 also introduced a standard way of declaring constructors and destructors.

As with other server-side scripting languages, several development frameworks have been developed for PHP to promote the rapid development and deployment of web applications. Repositories for PHP applications and extensions to the PHP programming language have also emerged. The PHP Extension and Application Repository (PEAR) was set up in 1999 as repository, package management and distribution system for PHP code libraries. Packages are distributed as gzipped .tar files that contain PHP code. The code can often be included in a PHP application using include statements, or the package may be installed on the server using the PEAR package manager included with PHP by default, which can be used to install or upgrade either PEAR or PECL extensions. PECL contains PHP extensions written in C, as mentioned above. Although conceptually similar to PEAR, it is actually a spin-off from PEAR that first appeared in 2003 and now operates independently. Although compiled into PHP, extensions written in C generally run more efficiently than most PEAR packages, and some have been incorporated into the PHP core libraries. In 2002, the PHP Quality Assurance Initiative was set up in response to criticism that PHP applications did not always meet the requirements of a production environment. Its activities include tracking down and fixing PHP software problems.

For PHP software, information, tutorials and much more, visit the PHP Group website at: http://www.php.net.