The PHP language evolved without a written
formal specification or standard until 2014, with the original implementation acting as the
de facto standard which other implementations aimed to follow. Since 2014 work has gone on to create a formal PHP specification.
[10]
During the 2010s there have been increased efforts towards standardisation and code sharing in PHP applications by projects such as PHP-FIG in the form of PSR initiatives as well as the
Composer dependency manager and associated Packagist repository. PHP hosts a diverse array of web frameworks requiring framework-specific knowledge, with
Laravel recently emerging as a popular option by incorporating ideas made popular from other competing non-PHP web frameworks, like
Ruby on Rails.
History
Early history
PHP/FI could be used to build simple, dynamic
web applications. To accelerate
bug reporting and improve the code, Lerdorf initially announced the release of PHP/FI as "Personal Home Page Tools (PHP Tools) version 1.0" on the
Usenet discussion group
comp.infosystems.www.authoring.cgi on June 8, 1995.
[14][15] This release already had the basic functionality that PHP has today. This included
Perl-like variables, form handling, and the ability to embed HTML. The
syntax resembled that of Perl, but was simpler, more limited and less consistent.
[6]
Early PHP was not intended to be a new programming language, and grew organically, with Lerdorf noting in retrospect: "I don't know how to stop it, there was never any intent to write a programming language [...] I have absolutely no idea how to write a programming language, I just kept adding the next logical step on the way."
[16] A development team began to form and, after months of work and
beta testing, officially released PHP/FI 2 in November 1997.
The fact that PHP was not originally designed, but instead was developed organically has led to inconsistent naming of functions and inconsistent ordering of their parameters.
[17] In some cases, the function names were chosen to match the lower-level libraries which PHP was "wrapping",
[18] while in some very early versions of PHP the length of the function names was used internally as a
hash function, so names were chosen to improve the distribution of hash values.
[19]
PHP 3 and 4

This is an example of custom PHP code for the CMS
WordPress.
On May 22, 2000, PHP 4, powered by the Zend Engine 1.0, was released.
[6] As of August 2008 this branch reached version 4.4.9. PHP 4 is no longer under development nor will any security updates be released.
[22][23]
PHP 5
On July 14, 2004, PHP 5 was released, powered by the new Zend Engine II.
[6] PHP 5 included new features such as improved support for
object-oriented programming, the PHP Data Objects (PDO) extension (which defines a lightweight and consistent interface for accessing databases), and numerous performance enhancements.
[24] In 2008 PHP 5 became the only stable version under development.
Late static binding had been missing from PHP and was added in version 5.3.
[25][26]
Many high-profile open-source projects ceased to support PHP 4 in new code as of February 5, 2008, because of the GoPHP5 initiative,
[27] provided by a consortium of PHP developers promoting the transition from PHP 4 to PHP 5.
[28][29]
Over time, PHP interpreters became available on most existing
32-bit and
64-bit operating systems, either by building them from the PHP source code, or by using pre-built binaries.
[30] For the PHP versions 5.3 and 5.4, the only available
Microsoft Windows binary distributions were 32-bit
x86 builds,
[31][32] requiring Windows 32-bit compatibility mode while using
Internet Information Services (IIS) on a 64-bit Windows platform. PHP version 5.5 made the 64-bit
x86-64 builds available for Microsoft Windows.
[33]
PHP 6 and Unicode
PHP received mixed reviews due to lacking native
Unicode support at the core language level.
[34][35] In 2005, a project headed by Andrei Zmievski was initiated to bring native Unicode support throughout PHP, by embedding the
International Components for Unicode (ICU) library, and representing text strings as
UTF-16 internally.
[36] Since this would cause major changes both to the internals of the language and to user code, it was planned to release this as version 6.0 of the language, along with other major features then in development.
[37]
However, a shortage of developers who understood the necessary changes, and performance problems arising from conversion to and from UTF-16, which is rarely used in a web context, led to delays in the project.
[38] As a result, a PHP 5.3 release was created in 2009, with many non-Unicode features back-ported from PHP 6, notably namespaces. In March 2010, the project in its current form was officially abandoned, and a PHP 5.4 release was prepared containing most remaining non-Unicode features from PHP 6, such as traits and closure re-binding.
[39] Initial hopes were that a new plan would be formed for Unicode integration, but as of 2014 none had been adopted.
PHP
During 2014 and 2015, a new major PHP version was developed, which was numbered PHP 7. The numbering of this version involved some debate.
[40] While the PHP 6 Unicode experiment had never been released, several articles and book titles referenced the PHP 6 name, which might have caused confusion if a new release were to reuse the name.
[41]After a vote, the name PHP 7 was chosen.
[42]
The foundation of PHP is a PHP
branch that was originally dubbed
PHP next generation (
phpng). It was authored by Dmitry Stogov, Xinchen Hui and Nikita Popov,
[43] and aimed to optimize PHP performance by refactoring the Zend Engine while retaining near-complete language compatibility.
[44] As of 14 July 2014,
WordPress-based benchmarks, which served as the main benchmark suite for the phpng project, showed an almost 100% increase in performance. Changes from phpng are also expected to make it easier to improve performance in the future, as more compact data structures and other changes are seen as better suited for a successful migration to a
just-in-time (JIT) compiler.
[45] Because of the significant changes, the reworked Zend Engine is called
Zend Engine 3, succeeding Zend Engine 2 used in PHP 5.
[46]
Because of major internal changes in phpng, it must receive a new
major version number of PHP, rather than a minor PHP 5 release, according to PHP's release process.
[47] Major versions of PHP are allowed to break backward-compatibility of code and therefore PHP 7 presented an opportunity for other improvements beyond phpng that require backward-compatibility breaks. In particular, it involved the following changes:
- Many fatal- or recoverable-level legacy PHP error mechanisms were replaced with modern object-oriented exceptions[48]
- The syntax for variable dereferencing was reworked to be internally more consistent and complete, allowing the use of the operators ->, [], (), {}, and :: with arbitrary meaningful left-hand-side expressions[49]
- Support for legacy PHP 4-style constructor methods was deprecated[50]
- The behavior of the foreach statement was changed to be more predictable[51]
- Constructors for the few classes built-in to PHP which returned null upon failure were changed to throw an exception instead, for consistency[52]
- Several unmaintained or deprecated server application programming interfaces (SAPIs) and extensions were removed from the PHP core, most notably the legacy mysqlextension[53]
- The behavior of the list() operator was changed to remove support for strings[54]
- Support for legacy ASP-style PHP code delimiters (<% and %>, <script language=php> and </script>) was removed[55]
- An oversight allowing a switch statement to have multiple default clauses was fixed[56]
- Support for hexadecimal number support in some implicit conversions from strings to number types was removed[57]
- The left-shift and right-shift operators were changed to behave more consistently across platforms[58]
- Conversions between integers and floating point numbers were tightened and implemented more consistently across platforms[58][59]
PHP 7 also included new language features. Most notably, it introduces return type declarations for functions
[60] which complement the existing parameter type declarations, and support for the
scalar types (integer, float, string, and boolean) in parameter and return type declarations.
Mascot

The elePHPant, PHP mascot.
The mascot of the PHP project is the
elePHPant, a blue
elephant with the PHP logo on its side, designed by Vincent Pontier
[96] in 1998.
[97] "The (PHP) letters were forming the shape of an elephant if viewed in a sideways angle."
[98] The elePHPant is sometimes differently colored when in
plush toy form.
Many variations of this mascot have been made over the years. Only the elePHPants based on the original design by Vincent Pontier are considered official by the community
[99]. These are highly collectible and some of them are extremely rare. Different variations are listed on A Field Guide to Elephpants.
Syntax
<!DOCTYPE html>
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo '<p>Hello World</p>'; ?>
</body>
</html>
However, as no requirement exists for PHP code to be embedded in HTML, the simplest version of
Hello, World! may be written like this, with the closing tag omitted as preferred in files containing pure PHP code
[100]
The PHP interpreter only executes PHP code within its
delimiters. Anything outside its delimiters is not processed by PHP, although non-PHP text is still subject to
control structuresdescribed in PHP code. The most common delimiters are
<?php to open and
?> to close PHP sections. The shortened form
<? also exists. This short delimiter makes script files less portable, since support for them can be disabled in the local PHP configuration and it is therefore discouraged.
[101][102] However, there is no recommendation against the use of the echo short tag
<?=.
[103] Prior to PHP 5.4.0, this short syntax for
echo() only works with the
short_open_tag configuration setting enabled, while for PHP 5.4.0 and later it is always available.
[104][105][101] The purpose of all these delimiters is to separate PHP code from non-PHP content, such as
JavaScript code or HTML markup.
[106]
The first form of delimiters,
<?php and
?>, in
XHTML and other
XML documents, creates correctly formed XML processing instructions.
[107] This means that the resulting mixture of PHP code and other markup in the server-side file is itself well-formed XML.
Variables are prefixed with a
dollar symbol, and a
type does not need to be specified in advance. PHP 5 introduced
type hinting that allows functions to force their parameters to be objects of a specific class, arrays, interfaces or
callback functions. However, before PHP 7.0, type hints could not be used with scalar types such as integer or string.
[61]
Unlike function and class names, variable names are case sensitive. Both double-quoted ("") and
heredoc strings provide the ability to interpolate a variable's value into the string.
[108] PHP treats
newlines as
whitespace in the manner of a
free-form language, and statements are terminated by a semicolon.
[109] PHP has three types of
comment syntax:
/* */
marks block and inline comments;
//
or
#
are used for one-line comments.
[110] The
echo
statement is one of several facilities PHP provides to output text,
e.g., to a web browser.
In terms of keywords and language syntax, PHP is similar to the C style syntax. if conditions, for and while loops, and function returns are similar in syntax to languages such as C, C++, C#, Java and Perl.
Data types
PHP stores integers in a platform-dependent range, either a 64-bit or 32-bit
signed integer equivalent to the
C-language long type. Unsigned integers are converted to signed values in certain situations; this behavior is different from other programming languages.
[111] Integer variables can be assigned using decimal (positive and negative),
octal,
hexadecimal, and
binary notations.
Floating point numbers are also stored in a platform-specific range. They can be specified using floating point notation, or two forms of
scientific notation.
[112] PHP has a native
Boolean type that is similar to the native Boolean types in
Java and
C++. Using the Boolean type conversion rules, non-zero values are interpreted as true and zero as false, as in
Perl and C++.
[112]
The null data type represents a variable that has no value;
NULL
is the only allowed value for this data type.
[112]
Variables of the "resource" type represent references to resources from external sources. These are typically created by functions from a particular extension, and can only be processed by functions from the same extension; examples include file, image, and database resources.
[112]
Arrays can contain elements of any type that PHP can handle, including resources, objects, and even other arrays. Order is preserved in lists of values and in
hashes with both keys and values, and the two can be intermingled.
[112] PHP also supports
strings, which can be used with single quotes, double quotes, nowdoc or
heredoc syntax.
[113]
The Standard PHP Library (SPL) attempts to solve standard problems and implements efficient data access interfaces and classes.
[114]
Functions
PHP defines a large array of functions in the core language and many are also available in various extensions; these functions are well documented in the online PHP documentation.
[115] However, the built-in library has a wide variety of naming conventions and associated inconsistencies, as described under
history above.
Custom functions may be defined by the developer, e.g.:
function myAge(Int $birthYear) : String
{ // defines a function, this one is named "myAge"
$yearsOld = date('Y') - $birthYear; // calculates the age
return $yearsOld . ' year' . ($yearsOld != 1 ? 's' : ''); // returns the age in a descriptive form
}
echo 'I am currently ' . myAge(1995) . ' old.'; // outputs the text concatenated
// with the return value of myAge()
// As the result of this syntax, myAge() is called.
In 2018, the output of the above sample program is 'I am currently 23 years old.'
In lieu of
function pointers, functions in PHP can be referenced by a string containing their name. In this manner, normal PHP functions can be used, for example, as
callbacks or within
function tables.
[116] User-defined functions may be created at any time without being
prototyped.
[115][116] Functions may be defined inside code blocks, permitting a
run-time decision as to whether or not a function should be defined. There is a
function_exists
function that determines whether a function with a given name has already been defined. Function calls must use parentheses, with the exception of zero-argument class
constructor functions called with the PHP operator
new, in which case parentheses are optional.
Until PHP 5.3, support for
anonymous functions and
closures did not exist in PHP. While
create_function()
exists since PHP 4.0.1, it is merely a thin wrapper around
eval()
that allows normal PHP functions to be created during program execution.
[117] PHP 5.3 added syntax to define an anonymous function or "closure"
[118] which can capture variables from the surrounding scope:
function getAdder($x) {
return function($y) use ($x) {
return $x + $y;
};
}
$adder = getAdder(8);
echo $adder(2); // prints "10"
In the example above,
getAdder()
function creates a closure using passed argument
$x
(the keyword
use
imports a variable from the lexical context), which takes an additional argument
$y
, and returns the created closure to the caller. Such a function is a first-class object, meaning that it can be stored in a variable, passed as a parameter to other functions, etc.
[119]
Unusually for a dynamically typed language, PHP supports type declarations on function parameters, which are enforced at runtime. This has been supported for classes and interfaces since PHP 5.0, for arrays since PHP 5.1, for "callables" since PHP 5.4, and scalar (integer, float, string and boolean) types since PHP 7.0.
[61] PHP 7.0 also has type declarations for function return types, expressed by placing the type name after the list of parameters, preceded by a colon.
[60] For example, the
getAdder
function from the earlier example could be annotated with types like so in PHP 7:
function getAdder(int $x): \Closure {
return function(int $y) use ($x) : int {
return $x + $y;
};
}
$adder = getAdder(8);
echo $adder(2); // prints "10"
echo $adder(null); // throws an exception because an incorrect type was passed
$adder = getAdder([]); // would also throw an exception
By default, scalar type declarations follow weak typing principles. So, for example, if a parameter's type is
int
, PHP would allow not only integers, but also convertible numeric strings, floats or booleans to be passed to that function, and would convert them.
[61] However, PHP 7 has a "strict typing" mode which, when used, disallows such conversions for function calls and returns within a file.
[61]
PHP Objects
Basic
object-oriented programming functionality was added in PHP 3 and improved in PHP 4.
[6] This allowed for PHP to gain further abstraction, making creative tasks easier for programmers using the language. Object handling was completely rewritten for PHP 5, expanding the feature set and enhancing performance.
[120] In previous versions of PHP, objects were handled like
value types.
[120] The drawback of this method was that code had to make heavy use of PHP's "reference" variables if it wanted to modify an object it was passed rather than creating a copy of it. In the new approach, objects are referenced by
handle, and not by value.
If the developer creates a copy of an object using the reserved word
clone
, the Zend engine will check whether a
__clone()
method has been defined. If not, it will call a default
__clone()
which will copy the object's properties. If a
__clone()
method is defined, then it will be responsible for setting the necessary properties in the created object. For convenience, the engine will supply a function that imports the properties of the source object, so the programmer can start with a by-value
replica of the source object and only override properties that need to be changed.
[122]
class Person
{
public $firstName;
public $lastName;
public function __construct($firstName, $lastName = '') { // optional second argument
$this->firstName = $firstName;
$this->lastName = $lastName;
}
public function greet() {
return 'Hello, my name is ' . $this->firstName .
(($this->lastName != '') ? (' ' . $this->lastName) : '') . '.';
}
public static function staticGreet($firstName, $lastName) {
return 'Hello, my name is ' . $firstName . ' ' . $lastName . '.';
}
}
$he = new Person('John', 'Smith');
$she = new Person('Sally', 'Davis');
$other = new Person('iAmine');
echo $he->greet(); // prints "Hello, my name is John Smith."
echo '<br />';
echo $she->greet(); // prints "Hello, my name is Sally Davis."
echo '<br />';
echo $other->greet(); // prints "Hello, my name is iAmine."
echo '<br />';
echo Person::staticGreet('Jane', 'Doe'); // prints "Hello, my name is Jane Doe."
The
visibility of PHP properties and methods is defined using the
keywords public
,
private
, and
protected
. The default is public, if only
var is used;
var
is a synonym for
public
. Items declared
public
can be accessed everywhere.
protected
limits access to
inherited classes (and to the class that defines the item).
private
limits visibility only to the class that defines the item.
[123] Objects of the same type have access to each other's private and protected members even though they are not the same instance.
Implementations
The original, only complete and most widely used PHP implementation is powered by the
Zend Engine and known simply as PHP. To disambiguate it from other implementations, it is sometimes unofficially called "Zend PHP". The Zend Engine
compiles PHP
source code on-the-fly into an internal format that it can execute, thus it works as an
interpreter.
[124][125] It is also the "reference implementation" of PHP, as PHP has no formal specification, and so the semantics of Zend PHP define the semantics of PHP. Due to the complex and nuanced semantics of PHP, defined by how Zend works, it is difficult for competing implementations to offer complete compatibility.
PHP's single-request-per-script-execution model, and the fact the Zend Engine is an interpreter, leads to inefficiency; as a result, various products have been developed to help improve PHP performance. In order to speed up execution time and not have to compile the PHP source code every time the web page is accessed, PHP scripts can also be deployed in the PHP engine's internal format by using an
opcode cache, which works by
caching the compiled form of a PHP script (opcodes) in
shared memory to avoid the overhead of
parsing and
compiling the code every time the script runs. An opcode cache,
Zend Opcache, is built into PHP since version 5.5.
[126] Another example of a widely used opcode cache is the
Alternative PHP Cache (APC), which is available as a
PECL extension.
[127]
While Zend PHP is still the most popular implementation, several other implementations have been developed. Some of these are
compilers or support
JIT compilation, and hence offer performance benefits over Zend PHP at the expense of lacking full PHP compatibility. Alternative implementations include the following:
- HHVM (HipHop Virtual Machine) – developed at Facebook and available as open source, it converts PHP code into a high-level bytecode (commonly known as an intermediate language), which is then translated into x86-64 machine code dynamically at runtime by a just-in-time (JIT) compiler, resulting in up to 6× performance improvements.[128]
- Parrot – a virtual machine designed to run dynamic languages efficiently; Pipp transforms the PHP source code into the Parrot intermediate representation, which is then translated into the Parrot's bytecode and executed by the virtual machine.
- Phalanger – compiles PHP into Common Intermediate Language (CIL) bytecode
- Quercus – compiles PHP into Java bytecode
- HipHop – developed at Facebook and available as open source, it transforms the PHP scripts into C++ code and then compiles the resulting code, reducing the server load up to 50%. In early 2013, Facebook deprecated it in favor of HHVM due to multiple reasons, including deployment difficulties and lack of support for the whole PHP language, including the create_function() and eval() constructs.[129]
Licensing
Products derived from this software may not be called "PHP", nor may "PHP" appear in their name, without prior written permission from group@php.net. You may indicate that your software works in conjunction with PHP by saying "
Foo for PHP" instead of calling it "PHP Foo" or "phpfoo".
This restriction on use of "PHP" makes the PHP License incompatible with the
General Public License (GPL), while the Zend License is incompatible due to an advertising clause similar to that of the original
BSD license.
[131]
Development and community
Some other projects, such as
Zephir, provide the ability for PHP extensions to be created in a high-level language and compiled into native PHP extensions. Such an approach, instead of writing PHP extensions directly in C, simplifies the development of extensions and reduces the time required for programming and testing.
[142]
Installation and configuration
PHP-FPM (FastCGI Process Manager) is an alternative FastCGI implementation for PHP, bundled with the official PHP distribution since version 5.3.3.
[146] When compared to the older FastCGI implementation, it contains some additional features, mostly useful for heavily loaded web servers.
When using PHP for command-line scripting, a PHP
command-line interface (CLI) executable is needed. PHP supports a CLI SAPI as of PHP 4.3.0.
[148] The main focus of this SAPI is developing
shell applications using PHP. There are quite a few differences between the CLI SAPI and other SAPIs, although they do share many of the same behaviors.
[149]
PHP has a direct module interface called SAPI for different web servers;
[150] in case of PHP 5 and Apache 2.0 on Windows, it is provided in form of a
DLL file called
php5apache2.dll,
[151] which is a module that, among other functions, provides an interface between PHP and the web server, implemented in a form that the server understands. This form is what is known as a SAPI.
PHP can also be used for writing desktop
graphical user interface (GUI) applications, by using the
PHP-GTK extension. PHP-GTK is not included in the official PHP distribution,
[145]and as an extension it can be used only with PHP versions 5.1.0 and newer. The most common way of installing PHP-GTK is compiling it from the source code.
[153]
Numerous configuration options are supported, affecting both core PHP features and extensions.
[156][157] Configuration file
php.ini
is searched for in different locations, depending on the way PHP is used.
[158] The configuration file is split into various sections,
[159] while some of the configuration options can be also set within the web server configuration.
Comments
Post a Comment