PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language. As of January 2013, PHP was installed on more than 240 million websites (39% of those sampled) and 2.1 million web servers. Originally created by Rasmus Lerdorf in 1995, the reference implementation of PHP is now produced by The PHP Group. While PHP originally stood for Personal Home Page, it now stands for PHP: Hypertext Preprocessor, a recursivebackronym.
PHP code is interpreted by a web server with a PHP processor module, which generates the resulting web page: PHP commands can be embedded directly into an HTMLsource document rather than calling an external file to process data. It has also evolved to include a command-line interface capability and can be used in standalonegraphical applications.
PHP is free software released under the PHP License. PHP can be deployed on most web servers and also as a standalone shell on almost every operating system andplatform, free of charge.
Contents
1 History
1.1 PHP 6 and Unicode
1.2 Release history
2 Syntax
2.1 Data types
2.2 Functions
2.3 Objects
3 Implementations
4 Licensing
5 Development and community
6 Installation and configuration
7 Use
8 Security
History
Rasmus Lerdorf, who wrote the original Common Gateway Interface (CGI) component, together with Andi Gutmans and Zeev Suraski, who rewrote the parser that formed PHP 3.
PHP development began in 1994 when the developer Rasmus Lerdorf wrote a series of Common Gateway Interface(CGI) Perl scripts, which he used to maintain his personal homepage. The tools performed tasks such as displaying his résumé and recording his web traffic. He rewrote these scripts in C for performance reasons, extending them to add the ability to work with web formsand to communicate with databases, and called this implementation "Personal Home Page/Forms Interpreter" or PHP/FI.
PHP/FI could be used to build simple, dynamic web applications. Lerdorf initially announced the release of PHP/FI as "Personal Home Page Tools (PHP Tools) version 1.0" publicly to accelerate bug location and improve the code, on the Usenet discussion group comp.infosystems.www.authoring.cgi on June 8, 1995. This release already had the basic functionality that PHP has as of 2013. 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.
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." A development team began to form and, after months of work and beta testing, officially released PHP/FI 2 in November 1997.
One criticism of PHP is that it was not originally designed, but instead it was developed organically;among other things, this has led to inconsistent naming of functions and inconsistent ordering of their parameters. In some cases, the function names were chosen to match the lower-level libraries which PHP was "wrapping", 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.
Zeev Suraski and Andi Gutmans rewrote the parser in 1997 and formed the base of PHP 3, changing the language's name to the recursive acronym PHP: Hypertext Preprocessor. Afterwards, public testing of PHP 3 began, and the official launch came in June 1998. Suraski and Gutmans then started a new rewrite of PHP's core, producing the Zend Engine in 1999. They also founded Zend Technologies in Ramat Gan, Israel.
On May 22, 2000, PHP 4, powered by the Zend Engine 1.0, was released. 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.
On July 13, 2004, PHP 5 was released, powered by the new Zend Engine II. 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.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
Many high-profile open-source projects ceased to support PHP 4 in new code as of February 5, 2008, because of the GoPHP5 initiative,provided by a consortium of PHP developers promoting the transition from PHP 4 to PHP 5.
PHP interpreters are 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. For the PHP versions 5.3 and 5.4, the only available Microsoft Windows binary distributions were 32-bit x86 builds, 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.
PHP 6 and Unicode
PHP received mixed reviews due to lacking native Unicode support at the core language level. 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. 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.
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. 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. Initial hopes were that a new plan would be formed for Unicode integration, but as of 2014 none has been adopted.
During the years before the release of PHP 5.3 and 5.4, some books were published based on the expected feature set of PHP 6.0, including both the Unicode work and the features which were later backported to other releases. There is therefore some debate over whether a new major version of PHP, with or without Unicode support, should be called "PHP 6", or if the version should be skipped to avoid confusion.
Release history
KeyColorMeaningDevelopment
Red Old release No development
Yellow Stable release Security fixes
Green Stable release Bug and security fixes
Blue Future release New features
VersionRelease dateSupported untilNotes
1.0 8 June 1995 Officially called "Personal Home Page Tools (PHP Tools)". This is the first use of the name "PHP".
2.0 1 November 1997
3.0 6 June 1998 20 October 2000 Development moves from one person to multiple developers. Zeev Suraski and Andi Gutmans rewrite the base for this version.
4.0 22 May 2000 23 January 2001 Added more advanced two-stage parse/execute tag-parsing system called the Zend engine.
4.1 10 December 2001 12 March 2002 Introduced 'superglobals' ($_GET, $_POST, $_SESSION, etc.)
4.2 22 April 2002 6 September 2002 Disabled register_globals by default. Data received over the network is not inserted directly into the global namespace anymore, closing possible security holes in applications.
4.3 27 December 2002 31 March 2005 Introduced the command-line interface (CLI), to supplement the CGI.
4.4 11 July 2005 7 August 2008 Fixed a memory corruption bug, which required breaking binary compatibility with extensions compiled against PHP version 4.3.x.
5.0 13 July 2004 5 September 2005 Zend Engine II with a new object model.
5.1 24 November 2005 24 August 2006 Performance improvements with introduction of compiler variables in re-engineered PHP Engine. Added PHP Data Objects (PDO) as a consistent interface for accessing databases.
5.2 2 November 2006 6 January 2011 Enabled the filter extension by default. Native JSON support.
5.3 30 June 2009 July 2014[43] Namespace support; late static bindings, Jump label (limited goto), Native closures, Native PHP archives (phar), garbage collection for circular references, improved Windows support, sqlite3, mysqlnd as a replacement for libmysql as underlying library for the extensions that work with MySQL, fileinfo as a replacement for mime_magic for better MIME support, the Internationalization extension, and deprecation of ereg extension.
5.4 1 March 2012 1 March 2015 Trait support, short array syntax support. Removed items: register_globals, safe_mode, allow_call_time_pass_reference, session_register(),session_unregister() and session_is_registered(). Built-in web server. Several improvements to existing features, performance and reduced memory requirements.
5.5 20 June 2013 20 June 2016 Support for generators, finally blocks for exceptions handling, OpCache (based on Zend Optimizer+) bundled in official distribution.
5.6 No date set 3 years after release Constant scalar expressions, variadic functions, argument unpacking, new exponentiation operator, extensions of the use operator, new phpdbg debugger as a SAPI module, and other smaller improvements.
Beginning on June 28, 2011, the PHP Group began following a timeline for when new versions of PHP will be released.Under this timeline, at least one release should occur every month. Once per year, a minor release should occur which can include new features. Every minor release should at least have 2 years of security and bug fixes, followed by at least 1 year of only security fixes, for a total of a 3 year release process for every minor release. No new features (unless small and self-contained) will be introduced into a minor release during the 3-year release process.
Syntax
Main article: PHP syntax and semantics
The following Hello world program is written in PHP code embedded in an HTML document:
<!DOCTYPE html> <html> <head> <title>PHP Test</title> </head> <body> <?php echo '<p>Hello World</p>'; ?> </body> </html>
However, as PHP does not need to be embedded in HTML or used with a web server, the simplest version of a Hello World program can be written like this, with the closing tag omitted as preferred in files containing pure PHP code(prior to PHP 5.4.0, this short syntax for echo() only works with the short_open_tag configuration setting enabled):
The following Hello world program is written in PHP code embedded in an HTML document:
<!DOCTYPE html> <html> <head> <title>PHP Test</title> </head> <body> <?php echo '<p>Hello World</p>'; ?> </body> </html>
However, as PHP does not need to be embedded in HTML or used with a web server, the simplest version of a Hello World program can be written like this, with the closing tag omitted as preferred in files containing pure PHP code(prior to PHP 5.4.0, this short syntax for echo() only works with the short_open_tag configuration setting enabled):
<?= 'Hello world';
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 structures described in PHP code). The most common delimiters are <?php to open and ?> to close PHP sections. <script language="php"> and </script> delimiters are also available, as are the shortened forms <? or <?= (which is used to echo back astring or variable) and ?> as well as ASP-style short forms <% or <%= and %>. While short delimiters are used, they make script files less portable as support for them can be disabled in the PHP configuration, and they are therefore discouraged.The purpose of all these delimiters is to separate PHP code from non-PHP code, including HTML.
The first form of delimiters, <?php and ?>, in XHTML and other XML documents, creates correctly formed XML "processing instructions". 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. 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. PHP treats newlines as whitespace in the manner of a free-form language (except when inside string quotes), and statements are terminated by a semicolon PHP has three types of comment syntax: /* */ marks block and inline comments; // as well as # are used for one-line comments.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 most high level languages that follow 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.
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 structures described in PHP code). The most common delimiters are <?php to open and ?> to close PHP sections. <script language="php"> and </script> delimiters are also available, as are the shortened forms <? or <?= (which is used to echo back astring or variable) and ?> as well as ASP-style short forms <% or <%= and %>. While short delimiters are used, they make script files less portable as support for them can be disabled in the PHP configuration, and they are therefore discouraged.The purpose of all these delimiters is to separate PHP code from non-PHP code, including HTML.
The first form of delimiters, <?php and ?>, in XHTML and other XML documents, creates correctly formed XML "processing instructions". 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. 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. PHP treats newlines as whitespace in the manner of a free-form language (except when inside string quotes), and statements are terminated by a semicolon PHP has three types of comment syntax: /* */ marks block and inline comments; // as well as # are used for one-line comments.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 most high level languages that follow 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 whole numbers 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.[57] 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. 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++.
The null data type represents a variable that has no value; NULL is the only allowed value for this data type.
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
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
PHP also supports strings, which can be used with single quotes, double quotes, nowdoc or heredoc syntax.
The Standard PHP Library (SPL) attempts to solve standard problems and implements efficient data access interfaces and classes.
The Standard PHP Library (SPL) attempts to solve standard problems and implements efficient data access interfaces and classes.
Function
PHP has hundreds of functions provided by the core language functionality and thousands more available via various extensions; these functions are well documented in the online PHP documentation. However, the built-in library has a wide variety of naming conventions and associated inconsistencies, as described under history above.
Additional functions can be defined by a developer:
function myFunction() // defines a function, this one is named "myFunction" { return 'John Doe'; // returns the value 'John Doe' } echo 'My name is ' . myFunction() . '!'; // outputs the text concatenated with the return value of myFunction. // myFunction() is called as the result of this syntax. // The result of the output will be 'My name is John Doe!'
In PHP 5.2 and earlier, functions are not first-class functions and can only be referenced by their name, directly or dynamically by a variable containing the name of the function.[61] User-defined functions can be created at any time without being prototyped.[61] Functions can be defined inside code blocks, permitting a run-time decision as to whether or not a function should be defined. Function calls must use parentheses, with the exception of zero-argument class constructor functions called with the PHP
new
operator, where parentheses are optional.
PHP supports quasi-anonymous functions through the
create_function()
function.[62] However, they are not truly anonymous because they can be referenced either by name, or indirectly as variable functions using $function_name()
syntax,[61][63] as seen in the following example:$f = create_function('$a, $b = 5', 'var_dump($a, $b);'); $f(1); // output: // int(1) // int(5)
PHP 5.3 added support for closures, which enable true anonymous functions.[64] The syntax can be seen in the following example:
function getAdder($x) { return function($y) use ($x) { return $x + $y; }; } $adder = getAdder(8); echo $adder(2); // prints "10"
Here, the
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. For more details, see the Lambda functions and closures RFC.
The
goto
flow control statement is used as in the following example:function lock() { $file = fopen('file.txt', 'r+'); retry: if (!flock($file, LOCK_EX | LOCK_NB)) goto retry; fwrite($file, 'Success!'); fclose($file); }
When
flock()
is called, PHP opens a file and tries to lock it. The target label retry:
defines the point to which execution should return if flock()
is unsuccessful and goto retry;
is called. The goto
statement is restricted and requires that the target label be in the same file and context.
The
goto
statement has been supported since PHP 5.3.Objects
Basic object-oriented programming functionality was added in PHP 3 and improved in PHP 4.[5] Object handling was completely rewritten for PHP 5, expanding the feature set and enhancing performance.[65] In previous versions of PHP, objects were handled like value types.[65] The drawback of this method was that the whole object was copied when a variable was assigned or passed as a parameter to a method. In the new approach, objects are referenced by handle, and not by value.
PHP 5 introduced private and protected member variables and methods, along with abstract classes, final classes, abstract methods, and final methods. It also introduced a standard way of declaring constructors anddestructors, similar to that of other object-oriented languages such as C++, and a standard exception handling model. Furthermore, PHP 5 added interfaces and allowed for multiple interfaces to be implemented. There are special interfaces that allow objects to interact with the runtime system. Objects implementing ArrayAccess can be used with array syntax and objects implementing Iterator or IteratorAggregate can be used with the
foreach
language construct. There is no virtual table feature in the engine, so static variables are bound with a name instead of a reference at compile time.[66]
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.[67]
The following is a basic example of object-oriented programming in PHP:
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 . '.'; } 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."
No comments:
Post a Comment