Jump to content

PHP: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Line 286: Line 286:
* {{dmoz|Computers/Programming/Languages/PHP/|PHP}}
* {{dmoz|Computers/Programming/Languages/PHP/|PHP}}
* [http://www.zercle.com/bg/fbgu-1b.shGrp.php?bg=721 PHP books (categorized; currently in-print)]
* [http://www.zercle.com/bg/fbgu-1b.shGrp.php?bg=721 PHP books (categorized; currently in-print)]

{{PHP}}

[[Category:Curly bracket programming languages]]
[[Category:Free compilers and interpreters]]
[[Category:Procedural programming languages]]
[[Category:Object-oriented programming languages]]
[[Category:PHP programming language]]
[[Category:Programming languages]]
[[Category:Scripting languages]]
[[Category:Acronyms]]

[[af:PHP]]
[[als:PHP]]
[[ar:بي.إتش.بي]]
[[an:PHP]]
[[az:PHP]]
[[bn:পিএইচপি]]
[[zh-min-nan:PHP]]
[[bs:PHP]]
[[br:Areg PHP]]
[[bg:PHP]]
[[ca:PHP]]
[[cs:PHP]]
[[cy:PHP]]
[[da:PHP]]
[[de:PHP]]
[[et:PHP]]
[[el:PHP]]
[[es:PHP]]
[[eo:PHP]]
[[eu:PHP]]
[[fa:زبان برنامه‌نویسی پی‌اچ‌پی]]
[[fr:PHP: Hypertext Preprocessor]]
[[gl:PHP]]
[[ko:PHP]]
[[hi:पीएचपी]]
[[hr:PHP]]
[[id:PHP]]
[[is:PHP]]
[[it:PHP]]
[[he:PHP]]
[[ka:PHP]]
[[kk:PHP]]
[[ku:PHP]]
[[la:PHP]]
[[lv:PHP]]
[[lb:PHP]]
[[lt:PHP]]
[[hu:PHP]]
[[mk:PHP]]
[[ms:PHP]]
[[nl:PHP]]
[[ja:PHP: Hypertext Preprocessor]]
[[no:PHP]]
[[pl:PHP]]
[[pt:PHP]]
[[ro:PHP]]
[[ru:PHP]]
[[sq:PHP]]
[[simple:PHP]]
[[sk:PHP (programovací jazyk)]]
[[sl:PHP]]
[[sr:PHP]]
[[sh:PHP]]
[[fi:PHP]]
[[sv:PHP]]
[[th:ภาษาพีเอชพี]]
[[vi:PHP]]
[[tg:PHP]]
[[tr:PHP]]
[[uk:PHP]]
[[vls:PHP]]
[[zh-yue:PHP]]
[[bat-smg:PHP]]
[[zh:PHP]]


{{PHP}}
{{PHP}}

Revision as of 06:07, 25 February 2008

PHP
File:Php-logo.gif
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 is a computer scripting language originally designed for producing dynamic web pages.[1] The name PHP is a recursive acronym for PHP: Hypertext Preprocessor. It is used mainly in server-side scripting, but can be used from a command line interface or in standalone graphical applications. Textual User Interfaces can also be created using ncurses.

The main implementation of PHP is produced by The PHP Group and released under the PHP License. This implementation serves to define a de facto standard for PHP, as there is no formal specification. The most recent version of PHP is 5.2.5, released on 8 November 2007. It is considered to be free software by the Free Software Foundation.[2]

History

Rasmus Lerdorf
Andi Gutmans, One of the creators of the new PHP as we know it today

PHP was written as a set of Common Gateway Interface (CGI) binaries in the C programming language by the Danish/Greenlandic programmer Rasmus Lerdorf in 1994, to replace a small set of Perl scripts he had been using to maintain his personal homepage.[3] Lerdorf initially created PHP to display his résumé and to collect certain data, such as how much traffic his page was receiving. Personal Home Page Tools was publicly released on 8 June 1995 after Lerdorf combined it with his own Form Interpreter to create PHP/FI (this release is considered PHP version 2).[4]

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. 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.[5] 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.[6]

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

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.[6] 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[8], many high profile open source projects ceased to support PHP 4 in new code as of 5 February 2008.[9]

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 [10] Added more advanced two-stage parse/execute tag-parsing system called the Zend engine.
PHP 4.1.0 December 10, 2001 [10] Introduced the superglobals ($_GET, $_POST, $_SESSION, etc.)
PHP 4.2.0 April 22, 2002 [10] Disabled register_globals by default
PHP 4.3.0 December 27, 2002 [10] Introduced the CLI, in addition to the CGI
PHP 4.4.0 July 11, 2005 [10]
PHP 5.0.0 July 13, 2004 [11] Zend Engine II with a new object model.
PHP 5.1.0 November 24, 2005 [11] Performance improvements with introduction of compiler (CV) variables in re-engineered PHP Engine.
PHP 5.2.0 November 2, 2006 [11] Enabled the filter extension by default

Popularity

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

Usage

PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. 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. 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. 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. 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. 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; other PHP frameworks along the same lines include CakePHP, PRADO and Symfony.

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.[14] The number of installations is different from the number of sites actually using those installations, but this statistic does reflect the popularity of PHP.

Examples of popular open source server-side PHP applications include phpBB, WordPress, and MediaWiki.

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.[15]

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.

Syntax

The usual Hello World code example for PHP is:

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

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.[16] The example above outputs the following:

Hello World!

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.

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 is used for inline comments.

It should be noted that many examples use the print function instead of the echo function; the two are practically identical, and one may decide which one to use based on personal preference.

Output of text in HTML requires explicit use of the htmlspecialchars() function. The program may otherwise output invalid HTML and make the website vulnerable to a cross-site scripting attack.

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.[17] 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. 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. 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.

Encoders hinder source code reverse engineering. Encoders fall broadly into two types; those that hide source code and those that compile code into bytecode. 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. 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. 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 and Notes

  1. ^ "Introduction". PHP Manual. Retrieved 2006-11-15.
  2. ^ "GPL-Incompatible, Free Software Licenses". Various Licenses and Comments about Them. Free Software Foundation. Retrieved 2008-02-22.
  3. ^ "History of PHP". The PHP Group. Retrieved 2007-12-26.
  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. ^ "Zend Engine version 2.0: Feature Overview and Design". Zend Technologies Ltd. Retrieved 2006-09-17.
  6. ^ a b "php.net 2007 news archive". The PHP Group. 2007-07-13. Retrieved 2008-02-22.
  7. ^ Trachtenberg, Adam (2004-07-15). "Why PHP 5 Rocks!". O'Reilly. Retrieved 2008-02-22.
  8. ^ 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)
  9. ^ "GoPHP5". GoPHP5. Retrieved 2008-02-22.
  10. ^ a b c d e "PHP: PHP 4 ChangeLog". The PHP Group. 2008-01-03. Retrieved 2008-02-22.
  11. ^ a b c "PHP: PHP 5 ChangeLog". The PHP Group. 2007-11-08. Retrieved 2008-02-22.
  12. ^ "PHP: PHP Usage Stats". SecuritySpace. 2007-04-01. Retrieved 2008-02-24.
  13. ^ "TIOBE Programming Community Index for February 2008". TIOBE. 2008-02-01. Retrieved 2008-02-24.
  14. ^ The PHP Group. "PHP Usage Stats for April 2007". PHP Website. Retrieved 2007-12-28.
  15. ^ Fuecks, Harry (2004-06-16). "PHP on the Command Line - Part 1". SitePoint. Retrieved 2008-02-22.
  16. ^ "Escaping from HTML". The PHP Group. Retrieved 2008-02-22.
  17. ^ "PHP: History of PHP and related projects - Manual". The PHP Group. Retrieved 2008-02-22.

Further reading