Jump to content

PHP

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by SuzieDerkins (talk | contribs) at 11:50, 8 March 2008 (→‎Popularity: typo). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

PHP
Paradigmimperative, object-oriented
Designed byRasmus Lerdorf
DeveloperThe PHP Group
First appeared1995
Stable release
Preview release
6.0-dev
Typing disciplineDynamic, weak
OSCross-platform
LicensePHP License
Websitehttp://php.net/
Major implementations
Roadsend PHP, Phalanger, Quercus, Project Zero
Influenced by
C, Perl, Java, C++, C#, Python
Influenced
Php4delphi

PHP (PHP: Hypertext Preprocessor) is a computer scripting language originally designed for producing dynamic web pages.[1] It is used mainly in server-side scripting, but can be used from a command line interface or in standalone graphical applications.

While PHP was originally created by Rasmus Lerdorf in 1994[2], the main implementation of PHP is now produced by The PHP Group and serves as the de facto standard for PHP, as there is no formal specification. Released under the PHP License, it is considered to be free software by the Free Software Foundation.[3]

History

Rasmus Lerdorf

PHP began as a set of Common Gateway Interface (CGI) binaries written in the C programming language in 1994 by the Danish/Greenlandic programmer Rasmus Lerdorf. Lerdorf initially created these Peronal Home Page Tools to replace a small set of Perl scripts he had been using to maintain his personal homepage, such as display his résumé, and record data like how much traffic his page was receiving.

He combined these binaries with his Form Interpreter to create PHP/FI which had much more functionality. It included a much larger C implementation which could communicate with databases and helped build simple, dynamic web applications. He released PHP publicly on 8 June 1995 to speed up the finding of bugs and improving the code.[4] This release is considered PHP version 2, and already had basic functionality that PHP has today. This includes Perl-like variables, form handling, and the ability to embed HTML. The syntax was similar to Perl but was more limited, simpler and less consistent.

Andi Gutmans

Zeev Suraski and Andi Gutmans, two Israeli developers at the Technion IIT, rewrote the parser in 1997 and formed the base of PHP 3, changing the language's name to the recursive initialism PHP: Hypertext Preprocessor.[5] The development team officially released PHP/FI 2 in November 1997 after months of beta testing. 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.[6] They also founded Zend Technologies in Ramat Gan, Israel, which actively manages the development of PHP.

In May 2000, PHP 4, powered by the Zend Engine 1.0, was released. The most recent update released by The PHP Group, is for the older PHP version 4 code branch which, as of January 2008, is up to version 4.4.8. PHP 4 will be supported by security updates until August 8, 2008.[7]

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 extension (which defines a lightweight and consistent interface for accessing databases), and numerous performance enhancements.[8]

Currently, PHP 5.x is the only stable version that is being actively developed; active development on PHP 4 ceased at the end of 2007. However, critical security updates for PHP 4 will be provided until August 8, 2008.[7] PHP 6 is currently under development. As a result of the GoPHP5 initiative, a consortium of PHP developers promoting the transition from PHP 4 to PHP 5[9], many high profile open source projects ceased to support PHP 4 in new code as of 5 February 2008.[10]

Release history

Version Release date Most important changes
PHP 1.0 June 8, 1995 Officially called "Personal Home Page Tools (PHP Tools)". This is the first use of the name "PHP".
PHP Version 2 (PHP/FI) April 16, 1996 Considered by its creator as the "fastest and simplest tool" for creating dynamic web pages .
PHP 3.0 June 6, 1998 Development moves from one person to multiple developers. Zeev Suraski and Andi Gutmans rewrite the base for this version.
PHP 4.0.0 May 22, 2000 [11] Added more advanced two-stage parse/execute tag-parsing system called the Zend engine.
PHP 4.1.0 December 10, 2001 [11] Introduced the superglobals ($_GET, $_POST, $_SESSION, etc.)
PHP 4.2.0 April 22, 2002 [11] Disabled register_globals by default
PHP 4.3.0 December 27, 2002 [11] Introduced the CLI, in addition to the CGI
PHP 4.4.0 July 11, 2005 [11]
PHP 5.0.0 July 13, 2004 [12] Zend Engine II with a new object model.
PHP 5.1.0 November 24, 2005 [12] Performance improvements with introduction of compiler (CV) variables in re-engineered PHP Engine.
PHP 5.2.0 November 2, 2006 [12] Enabled the filter extension by default

Popularity

PHP is currently the most popular Apache module among all servers using Apache as a web server.[13] Among all currently existing computer programming languages, it is considered the fourth most popular, ranked only behind Java, C, and Visual Basic.[14]

A paradoxical indicator of PHP popularity is the number of software vulnerabilities related to PHP applications and identified by a CVE, which amounted to 12% in 2003, 20% in 2004, 28% in 2005, 43% in 2006, 36% in 2007, 38% for the first two months of 2008... More than one quarter of all-time listed software vulnerabilities are related to PHP, more than one third in recent years, and most of these defects can be exploited remotely. These vulnerabilities are triggered by bad programming habits (especially a lack of input sanitization) and doubtful features and configurations of the language (e.g. now deprecated register globals[15]).

Usage

PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.[16] PHP generally runs on a web server, taking PHP code as its input and creating Web pages as output. However, it can also be used for command-line scripting and client-side GUI applications. PHP can be deployed on most web servers and on almost every operating system and platform free of charge. The PHP Group also provides the complete source code for users to build, customize and extend for their own use.

PHP primarily acts as a filter.[17] The PHP program takes input from a file or stream containing text and special PHP instructions and outputs another stream of data for display.

From PHP 4, the PHP parser compiles input to produce bytecode for processing by the Zend Engine, giving improved performance over its interpreter predecessor.[18] PHP 5 uses the Zend Engine II.

Server-side scripting

Originally designed to create dynamic web pages, PHP's principal focus is server-side scripting.[19] While running the PHP parser with a web server and web browser, the PHP model can be compared to other server-side scripting languages such as Microsoft's ASP.NET system, Sun Microsystems' JavaServer Pages, and mod_perl as they all provide dynamic content to the client from a web server.[20] To more directly compete with the "framework" approach taken by these systems, Zend is working on the Zend Framework - an emerging (as of June 2006) set of PHP building blocks and best practices;[21] other PHP frameworks along the same lines include CakePHP, PRADO and Symfony.[22]

The LAMP architecture has become popular in the Web industry as a way of deploying inexpensive, reliable, scalable, secure web applications. PHP is commonly used as the P in this bundle alongside Linux, Apache and MySQL, although the P can also refer to Python or Perl. PHP can be used with a large number of relational database management systems, runs on all of the most popular web servers and is available for many different operating systems. This flexibility means that PHP has a wide installation base across the Internet; As of April 2007, over 20 million Internet domains were hosted on servers with PHP installed.[23] The number of installations is different from the number of sites actually using those installations, but this statistic does reflect the popularity of PHP.

Command-line scripting

PHP also provides a command line interface SAPI for developing shell and desktop applications, daemons, log parsing, or other system administration tasks that have traditionally been the domain of Perl, Python, awk, or shell scripting.[24]

Client-side GUI applications

PHP provides bindings to GUI libraries such as GTK+ (with PHP-GTK), Qt with PHP-Qt and text mode libraries like ncurses in order to facilitate development of a broader range of cross-platform GUI applications.[25]

Syntax

PHP only parses code within its delimiters. Anything outside its delimiters is sent directly to the output and not parsed by PHP. PHP supports a number of different delimiters. The most common delimiters are <?php and ?>, respectively open and close delimiters. <script language="php"> </script> style delimiters are also always available, so these two forms are the most portable. Short tags (<? or <?= and ?>) are also quite commonly used, but are along with ASP style tags (<% or <%= and %>) less portable, as they can be disabled in the PHP configuration. For this reason the use of Short tags and ASP style tags is discouraged.[26] The purpose of these delimiters is to separate PHP code from non-PHP code (notably HTML). Note that the delimiters are required to process PHP statements. Everything outside the delimiters is ignored by the parser and is simply passed through as output.[27]

The usual Hello World code example for PHP is:[28]

<?php
echo "Hello World!\n";
?>

The example above outputs the following:

Hello World!

Instead of using <? and the echo statement an optional "shortcut" is the use of <?= instead of <? which implicitly echoes data:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">
<html>
 <head>
 <title><?=$page_title;?></title>
 </head>
 <body>
 <p>Hello</p>
 </body>
</html>

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 allow the ability to embed the variable's value into the string.

PHP treats newlines as whitespace, in the manner of a free-form language (except when inside string quotes). Statements are terminated by a semicolon, except in a few special cases.

PHP has three types of comment syntax: /* */ which serves as block comments, and // as well as # which are used for inline comments.

It should be noted that many examples use the print function instead of the echo function. Both functions are nearly identical; the major difference being that print is slower than echo because the former will return a status indicating if it was successful or not in addition to text to output, whereas the latter doesn't return a status and only returns the text for output.

Data types

PHP stores whole numbers in a platform-dependent range. This range is typically that of 32-bit signed integers. Integer variables can be assigned using decimal (positive and negative), octal and hexadecimal notations. Real 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, named "boolean", 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.

There are eight data types in PHP:

  1. Integer
  2. Double
  3. Boolean
  4. String
  5. Object
  6. Array
  7. Null
  8. Resource

The null data type represents a variable that has no value. The only value in the null data type is NULL.

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 support both numeric and string indices, and are heterogeneous. 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. Objects can syntactially be used as Arrays.

Functions

PHP has hundreds of base functions and several thousand from extensions. Functions are not first-class functions and can only be referenced by their name.

User-defined functions can be created at any time and without being prototyped. An example function definition is the following:

<?php
function hello()
{
 echo "Hello World!\n";
}

hello();
?>

Functions can be defined inside code blocks, permitting a run-time decision as to whether or not a function should be defined. There is no concept of local functions. All 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. They are not true anonymous functions because anonymous functions are nameless but functions can only be referenced by name in PHP.

Function calls may be made via variables, where the value of a variable contains the name of the function to call. This is illustrated in the following example:

<?php
function hello()
{
 return 'Hello';
}
function world()
{
 return "World!\n";
}

$fn1 = 'hello';
$fn2 = 'world';

echo $fn1() . ' ' . $fn2();

?>

Objects

Basic object-oriented programming functionality was added in PHP 3.[29] Object handling was completely rewritten for PHP 5, expanding the feature set and enhancing performance. In previous versions of PHP, objects were handled like primitive types. 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 and final classes as well as abstract methods and final methods. It also introduced a standard way of declaring constructors and destructors, similar to that of other object-oriented languages such as C++, and a standard exception handling model. Furthermore PHP 5 added Interfaces and allows 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.

The static method and class variable features in Zend Engine 2 do not work the way some would expect. There is no virtual table feature in the engine, so static variables are bound with a name instead of a reference at compile time.

This example shows how to define a class, foo, that inherits from class bar. The function mystaticfunc is a public static function that can be called with foo::mystaticfunc();.

class foo extends bar
{
 function __construct()
 {
 $doo = "wah dee dee";
 }
 public static function mystaticfunc()
 {
 $dee = "dee dee dum";
 }
}

If the developer creates a copy of an object using the reserved word clone, the Zend engine will check if a __clone() method has been defined or not. If not, it will call a default __clone() which will copy all of 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 all properties of the source object, so that the programmer can start with a by-value replica of the source object and only override properties that need to be changed.

Resources

Libraries

PHP includes a large number of free and open source libraries with the core build. PHP is a fundamentally Internet-aware system with modules built in for accessing FTP servers, many database servers, embedded SQL libraries such as embedded MySQL and SQLite, LDAP servers, and others.[30] Many functions familiar to C programmers such as those in the stdio family are available in the standard PHP build.

Extensions

PHP allows developers to write extensions in C to add functionality to the PHP language.[31] These can then be compiled into PHP or loaded dynamically at runtime. Extensions have been written to add support for the Windows API, process management on Unix-like operating systems, multibyte strings (Unicode), cURL, and several popular compression formats. Some more unusual features include integration with Internet relay chat, dynamic generation of images and Adobe Flash content, and even speech synthesis. The PHP Extension Community Library (PECL) project is a repository for extensions to the PHP language.

Source code modifications

As with many scripting languages, PHP scripts are normally kept as human-readable source code, even on production webservers. While this allows flexibility, it can raise issues with security and performance[citation needed].

Encoders hinder source code reverse engineering. Encoders fall broadly into two types; those that hide source code and those that compile code into bytecode.[32] The downside of this latter approach is that a special extension has to be installed on the server in order to run encoded scripts, however the approach of encoding compiled code and use of an extension offers typically the best performance, security and opportunity for additional features that may be useful for developers. Compiled code solutions may exploit the potential for increased security through the use of their own execution engine, although some simpler solutions rely on the regular PHP engine to execute the compiled code. The most commonly used packages for source code protection are from Zend Technologies and ionCube Ltd.

Code optimizers improve the quality of the compiled code by reducing its size and making changes that can reduce the execution time and improve performance.[33] The nature of the PHP compiler is such that there are often many opportunities for code optimization.

Accelerators offer performance gains by caching the compiled form of a PHP script in shared memory to avoid the overhead of parsing and compiling the code every time the script runs.[34] They may also perform code optimization to provide increased execution performance. Both commercial (e.g. Zend Platform) and open source accelerators (e.g. xcache, eAccelerator, APC) are available.

See also

References

  1. ^ "Introduction". PHP Manual. Retrieved 2006-11-15.
  2. ^ "History of PHP". The PHP Group. Retrieved 2007-12-26.
  3. ^ "GPL-Incompatible, Free Software Licenses". Various Licenses and Comments about Them. Free Software Foundation. Retrieved 2008-02-22.
  4. ^ Lerdorf, Rasmus (1995-06-08). "Announce: Personal Home Page Tools (PHP Tools)". Newsgroupcomp.infosystems.www.authoring.cgi. Retrieved 2006-09-17. {{cite newsgroup}}: Check date values in: |date= (help)
  5. ^ "History of PHP and related projects". The PHP Group. Retrieved 2008-02-25.
  6. ^ "Zend Engine version 2.0: Feature Overview and Design". Zend Technologies Ltd. Retrieved 2006-09-17.
  7. ^ a b "php.net 2007 news archive". The PHP Group. 2007-07-13. Retrieved 2008-02-22.
  8. ^ Trachtenberg, Adam (2004-07-15). "Why PHP 5 Rocks!". O'Reilly. Retrieved 2008-02-22.
  9. ^ GoPHP5. "PHP projects join forces to Go PHP 5" (PDF). GoPHP5 Press Release. Retrieved 2008-02-23.{{cite web}}: CS1 maint: numeric names: authors list (link)
  10. ^ "GoPHP5". GoPHP5. Retrieved 2008-02-22.
  11. ^ a b c d e "PHP: PHP 4 ChangeLog". The PHP Group. 2008-01-03. Retrieved 2008-02-22.
  12. ^ a b c "PHP: PHP 5 ChangeLog". The PHP Group. 2007-11-08. Retrieved 2008-02-22.
  13. ^ "PHP: PHP Usage Stats". SecuritySpace. 2007-04-01. Retrieved 2008-02-24.
  14. ^ "TIOBE Programming Community Index for March 2008". TIOBE. 2008-03-01. Retrieved 2008-03-07.
  15. ^ "Using Register Globals". 2008-03-08. Retrieved 2008-03-08.
  16. ^ "Embedding PHP in HTML". O'Reilly. 2001-05-03. Retrieved 2008-02-25.
  17. ^ "What does PHP do?". The PHP Group. Retrieved 2008-02-25.
  18. ^ "PHP and MySQL". University of Alabama. Retrieved 2008-02-25.
  19. ^ "PHP Server-Side Scripting Language". Indiana University. 2007-04-04. Retrieved 2008-02-25.
  20. ^ "JavaServer Pages[tm] Technology - JavaServer Pages Comparing Methods for Server-Side Dynamic Content White Paper". Sun Microsystems. Retrieved 2008-02-25.
  21. ^ "Why Zend Framework?". Zend Technologies. Retrieved 2008-02-25.
  22. ^ "CakePHP". CakePHP. Retrieved 2008-02-25.
  23. ^ The PHP Group. "PHP Usage Stats for April 2007". PHP Website. Retrieved 2007-12-28.
  24. ^ Fuecks, Harry (2004-06-16). "PHP on the Command Line - Part 1". SitePoint. Retrieved 2008-02-22.
  25. ^ "What is PHP-GTK?". PHP GTK. Retrieved 2008-02-25.
  26. ^ "Escaping from HTML". The PHP Group. Retrieved 2008-02-22.
  27. ^ "Your first PHP-enabled page". The PHP Group. Retrieved 2008-02-25.
  28. ^ "Hello World". Code Newbie. Retrieved 2008-02-25.
  29. ^ "PHP: History of PHP and related projects - Manual". The PHP Group. Retrieved 2008-02-22.
  30. ^ "PHP Function List". The PHP Group. Retrieved 2008-02-25.
  31. ^ "Developing Custom PHP Extensions". devnewz. 2002-09-09. Retrieved 2008-02-25.
  32. ^ "bcompiler". PECL. Retrieved 2008-02-25.
  33. ^ "A HOWTO on Optimizing PHP". phpLens. Retrieved 2008-02-25.
  34. ^ "eAccelerator". eAccelerator. Retrieved 2008-02-25.

Further reading