×
Author:
Website:
Page title:
URL:
Published:
Last revised:
Accessed:

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

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 many 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 (now just Zend), which has its headquarters in California and a technology centre in Israel. Support for versions 3 and 4 ended in 2000 and 2008 respectively.

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.

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, including 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.

The PHP Data Objects (PDO) extension introduced with PHP 5 provides a lightweight database access interface, and various performance enhancements. Official support for all versions of PHP 5 had ended by the end of 2019.

PHP 6 is known as the "missing version number" in some developer circles. It was primarily an attempt to provide support for Unicode text strings, but this proved to be so problematical that PHP 6 was effectively abandoned in 2010, and was never officially released. Those (non-Unicode) features that were deemed worthy of salvaging were back-ported to version 5 and released as PHP 5.3.

The next major new version of PHP was released at the end of 2015. By that time, and despite the fact that PHP 6 was never officially released, a considerable amount of documentation had already been published relating to PHP 6. Since there were considerable differences between this new version and the doomed version 6, and in order to avoid any possible confusion, it was decided to skip one major version number and call the new version PHP 7.

For PHP 7, the Zend Engine was effectively re-engineered in order to optimise performance, while at the same time retaining almost complete backwards compatibility in terms of the language itself. In fact, the performance gains achieved led at one time to the new version being dubbed PHP next generation. Indeed, benchmarks based on the WordPress content management system displayed a near 100% improvement in performance.

As well as performance enhancements, the new version corrected a number of inconsistencies, and removed or deprecated several features that were considered to be either obsolete or anachronistic. New language features included the introduction of a return type declaration for functions, and support for scalar types (integer, float, string and Boolean) in for function parameters and return types.

The current major version of PHP is PHP 8, first released in late 2020. New features include the just in time compiler, named arguments, attributes, union types, weak maps, and constructor properties. The new features represent major improvements and a few syntax changes. Minor changes include improved error handling, changes to the way in which some operators work, and behind-the-scenes measures designed to reduce the chances of bugs being overlooked. The current stable version at the time of writing (June 2025) is PHP 8.4.8.

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.

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 which browser the user is currently using. The echo statement has been used in this example to output text in the web browser window.

<?php
echo 'You are using ';
if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE):
echo 'Internet explorer.';
elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== FALSE):
echo 'Mozilla Firefox.';
elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== FALSE):
echo 'Google Chrome';
elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== FALSE):
echo 'Opera Mini.';
elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== FALSE):
echo 'Opera.';
elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== FALSE):
echo 'Safari.';
else:
echo 'a browser unknown to us.';
endif;
?>

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.

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. */

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 an embedded PHP script</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 look like this:

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

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

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

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), which was set up in 2003, provides a repository for such extensions to the PHP language. PECL is actually a spin-off from the PHP Extension and Application Repository (PEAR), a repository of PHP software code set up by Stig S. Bakken in 1999 to promote the re-use of code that performs common functions, although PECL now operates independently.

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

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

PHP is now developed and maintained by the PHP Group as a de facto standard. For PHP software, information, tutorials and much more, visit the PHP Group website at: http://www.php.net. 2021 saw the formation of the PHP Foundation (https://thephp.foundation), whose stated aim is to sponsor the design and development of PHP by hiring developers to work on the language's core repository.