Jump to content

PHP

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Unknown W. Brackets (talk | contribs) at 17:42, 17 December 2004 (Revert...). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

For the "PHP" Cold-war history project, see Parallel History Project.

PHP (a recursive acronym for "PHP: Hypertext Preprocessor"; actually a retronym (see history)) is a widely-used open-source programming language primarily for server-side applications and developing dynamic web content. Famous examples of PHP applications include phpBB and MediaWiki, the software behind Wikipedia. The PHP model can be seen as an alternative to Microsoft's ASP/VBScript/JScript system, Macromedia's ColdFusion system, Sun Microsystems' JSP/Java system, and to the CGI/Perl system.

File:Php-med-trans.png
PHP logo

PHP's ease of use and similarity with the most common structured programming languages – most notably C and Perl (and from version 5, Java) – allows most experienced programmers to start developing complex applications with a minimal learning curve. It also enables experienced developers to get involved with dynamic web content applications without having to learn a whole new set of functions and practices.

One of the more attractive parts of PHP is that it is more than just a scripting language. Due to its modular design, PHP is also used to develop GUI applications (using PHP-GTK), and can be used from the command line just like Perl or Python.

PHP allows interaction with a large number of relational database systems, such as MySQL, Oracle, DB2, MS SQL Server, and PostgreSQL, while maintaining a simple and straightforward syntax. PHP runs on most major operating systems, including UNIX, Linux, Windows, and Mac OS X, and can interact with many major web servers. The official PHP website contains extensive documentation. The Linux, Apache, MySQL, PHP (LAMP) architecture has become popular in the Web industry as a way of deploying inexpensive, reliable, scalable, secure web applications. (The 'P' in LAMP can also stand for Perl or Python.)

PHP is the result of the collective efforts of many contributors. It is licensed under a BSD-style license, the PHP license. PHP, from version 4, has been powered by the Zend engine.

History

PHP was originally designed as a small set of Perl scripts, followed by a rewritten set of CGI binaries written in C by Rasmus Lerdorf in 1994 to display his résumé and collect some data, such as how many hits it was generating. Others first used "Personal Home Page Tools" in 1995, when Lerdorf had combined it with his own Form Interpreter to create PHP/FI. Zeev Suraski and Andi Gutmans, two Israeli developers of the Technion - Israel Institute of Technology, rewrote the parser in 1997 and formed the base of PHP 3. They also changed the name to its current recursive form. After months in beta, the development team officially released PHP/FI 2 in November 1997. Public testing of PHP 3 began immediately 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 (a page at www.zend.com states that PHP 3 was powered by Zend Engine 0.5). They also founded Zend Technologies in Ramat Gan, Israel which has since overseen the PHP advances. In May 2000, PHP 4, powered by the Zend Engine 1.0, was released. On July 13, 2004, PHP 5 was released, powered by Zend Engine II (formerly known as Zend Engine 2).

Popularity

PHP is currently one of the most popular server-side scripting systems on the Web. It has been widely adopted since the release of version 4, which was the first version powered by the powerful Zend Engine.

One major part of PHP which has helped it become popular is that it is a very loose language; in particular, it is dynamically typed. That is, the rules aren't as strict with variables—they don't have to be declared and they can hold any type of object. Further, unlike many other languages (like C++ and Java), arrays are able to hold objects of varying types, including other arrays.

According to Netcraft's April 2002 survey, PHP is now the most deployed server-side scripting language, running on around 9 of the 37 million domains in their survey. This is confirmed by PHP's own figures, which show PHP usage (measured on a per-domain basis) growing at around 5% per month. In May 2003, almost 13 million domains were using PHP, based on the same source.[1]

Due to PHP's popularity, a new breed of programmer has emerged – one who is only familiar with PHP, which in turn forced open the door toward a command line interface for PHP, along with support for GUI library such as GTK+ and text mode libraries like ncurses and newt. This is a major step for PHP, because it represents its beginning adoption as a genuine programming language (i.e. running autonomously on a stand-alone machine, as opposed to its original purpose of serving web pages to client machines from a server).

Many PHP programmers have reported having had trouble trying to learn other languages in the past and ultimately giving up after each attempt until attempting to learn PHP. The ease of programming in PHP has made it so these programmers are able to learn the basics of programming and are then able to continue on to other languages suchs as C/C++ or Perl/Python/Java and then finding themselves back programming in PHP for its speed of development in comparison to other languages.

Code example

Here is an example that prints out the lyrics for the song 99 Bottles of Beer:


<?php
/*
 * This is a comment. Other ways of commenting are // and # symbols.
 * This kind of comment does not need stars (*) in the beginning
 * of each line but including them is a common practice. // and # comments
 * only comment the text that are after them in the same line and have no
 * special ending character.
 */

/*
 * First we define a new function called "plural".
 * It will return an "s" if the argument passed to it was any other
 * than number 1.
 */
function plural($number) {
   return ($number != 1 ? "s" : "");
   // The ternary operation above is a conditional structure similar to if-else: (condition ? true : false)
}

// We define a variable called $l to contain an HTML line break
// as well as a carriage return and line feed:
$l = "<br />\r\n";

for ($i = 99; $i > 0; $i--) {
    print "$i bottle" . plural($i) . " of beer on the wall,$l";
    // We don't actually need a new print for each line. Let's see:
    print "$i bottle" . plural($i) . " of beer.$l
           Take one down, pass it around,$l" .
           ($i - 1 != 0 ? $i - 1 : "no more") .
           " bottle" . plural($i - 1) . " of beer on the wall.$l$l";

    /*
     * PHP allows you to put strings on multiple lines, as long as
     * it eventually finds a semicolon (;) to terminate it.
     * A period (.) joins (concatenates) strings together.
     * Variables, the $-symbol things, are parsed (interpolated)
     * inside double quotation marks ("), but wouldn't be parsed
     * inside single quotation marks ('). Functions, such as
     * plural(), are not parsed inside any quotation marks.
     */
}

print "Go to the store,$l buy some more,$l 99 bottles of beer on the wall!";

?>

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 like embedded MySQL and SQLite, LDAP servers, and others. Many functions familiar to C programmers such as the printf family are available in the standard PHP build.

PHP extensions exist which, among other features, add support for the Windows API, process management on UNIX-like operating systems, cURL, and the ZIP/gzip/bzip2/rar/lzf compression formats. Some of the more unusual features are on-the-fly Macromedia Flash generation, integration with Internet relay chat, and generation of dynamic images (where the content of the image can be changed). Some additional extensions are available via the PHP Extension Community Library (PECL).

This is the present list of all officially documented libraries:

(Source: PHP.net manual)

Object-oriented programming

Up until version 3, PHP had no object-oriented features. In version 3 basic object functionality was added. The same semantics were implemented in PHP 4 as well as pass-by-reference and return-by-reference for objects but the implementation still lacked the powerful and useful features of other object-oriented languages like C++ and Java.

In version 5, which was released in July 2004, PHP's object-oriented functionality has been very much enhanced and is more robust and complete. Here is a summary of some of the changes in PHP 5 (powered by Zend Engine II):

  • New Object Model — PHP's handling of objects has been completely rewritten, allowing for better performance and more features. In previous versions of PHP, objects were handled like primitive types (for instance integers and strings). The drawback of this method was that semantically 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 (one can think of a handle as an object's identifier).
  • Private and Protected Members — PHP 5 introduces private and protected member variables, they allow you to define the visibility of class properties.
  • Private and Protected Methods — Private and protected methods are also introduced.
  • Abstract Classes and Methods — PHP 5 also introduces abstract classes and methods. An abstract method only declares the method's signature and does not provide an implementation. A class that contains abstract methods needs to be declared abstract.
  • Interfaces — A class may implement an arbitrary list of interfaces.
  • Object Cloning — If the developer asks to create a copy of an object by 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 to set the necessary properties in the created object. For convenience, the engine will supply a function that imports all of the properties from the source object, so that they can start with a by-value replica of the source object, and only override properties that need to be changed.
  • Unified Constructors — PHP 5 introduces a standard way of declaring constructor methods by calling them by the name __construct().
  • Destructors — PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as Java: When the last reference to an object is destroyed, the object's destructor (a class method named __destruct() that receives no parameters) is called before the object is freed from memory.
  • Exceptions — PHP 4 had no exception handling. PHP 5 introduces a exception model similar to that of other programming languages.

More additions and examples of the additions mentioned above are available on this page.

It is should be noted that the static method and class variable features in Zend Engine 2 do not work the way some expect. There is no virtual table feature in the Engine, so the static variables are bound with a name at compile time instead of with a reference. This can lead to unexpected behavior, if you do not understand this.

Criticism

Criticism of PHP includes those general criticisms ascribed to other scripting programming languages and dynamically typed languages. In addition, specific criticism of PHP includes:

Syntax

  • PHP does not enforce the declaration of variables, and variables that have not been initialized can have operations (such as concatenation) performed on them. This leads to security holes with register_globals, as mentioned below.
  • Within sections of the built-in function selection there is little or no consistency regarding argument order (examples: order of subject array and other data for array handling functions, order of needle and haystack in various search functions).

Built-in functions

  • Built-in function names have no standard form, with some employing underscores (strip_tags) while others do not (stripslashes).
  • Some functions have inconsistent output. Statements like This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". can be found in the documentation. This is related to PHP's dynamic typing.
  • In some areas the selection of built-in functions is notably incomplete (supplying intersection and union functions, but no full difference function, for example).
  • The number of built-in functions is said to be too numerous, with many functions performing the same actions, but with just slightly different data, results, etc. This is said to make it difficult to program in the language without the frequent consultation of a reference work.
  • There are over 3,000 functions, sharing the same global namespace.
  • Some default settings and features are said to be confusing and the cause of frequent errors.
  • There is a "magic-quotes" feature that inserts backslashes into user-input strings. The feature was introduced to reduce code written by beginners from being dangerous (such as in SQL injection attacks), but some criticize it as a frequent cause of improperly displayed text.

Security

  • If register_globals is enabled in PHP's configuration file, users could cause harm by manipulating poorly written code. As of version 4.2.0 register_globals defaults to off.
  • Other languages, such as ASP.NET, include functionality to detect and clean harmful cross-site scripting or other malicious code automatically, whereas PHP does not.
  • In the majority of cases, Linux and Unix webservers with PHP installed (using mod_php) typically run PHP scripts as "nobody", which can make file security in a shared hosting environment difficult.

Miscellaneous

  • Error messages are said to be confusing; however, this is a common criticism alleged against many programming languages.

Support

PHP is self-supporting with respect to user support. Direct, one-to-one help is frequently provided free of charge through all of these and other mediums. PHP users assist each other through various media such as chat, forums, newsgroups and PHP developer web sites. In turn, the PHP development team actively participates in such communities, garnering assistance from them in their own development effort (PHP itself) and providing assistance to them as well. There are many help resources available for the novice PHP programmer.

These resources include:

  • Online Forums
  • - Online forum communities have sprung up around PHP support, such as:
  • php.net's manual, which also contains user-contributed notes function at the bottom of each manual page.
  • Devshed
  • PHPBuilder.
  • Listservs/Mailing_lists
  • PHP mailing lists
  • also available on PHP.net's news server
  • Newsgroups/Usenet
  • - the group comp.lang.php
  • Chat / IRC
  • - the IRC channels #php and #phphelp on EFNet, IRCNet, DALnet and other networks.

    - Other IRC channels include #php and #phpfreaks on freenode and also #phpcafe on irc.invisionize.com.

  • User Groups
  • - There is a PHP user group registry.

    Software built with PHP

    The following is list of notable software developed using PHP:

  • CMSformE
  • - Content management system
  • Drupal
  • - Content management system
  • Gallery Project
  • - Gallery software
  • Coppermine Photo Gallery
  • - Gallery software
  • Invision Power Board
  • - Internet forum software
  • Mambo Open Source
  • - Content management system
  • MediaWiki
  • - Wiki software
  • Moodle
  • - E-learning platform
  • phpBB
  • - Internet forum software
  • phpLDAPadmin
  • - LDAP management frontend
  • phpMyAdmin
  • - MySQL database management frontend
  • PHP-Nuke
  • - Content management system
  • phpPgAdmin
  • - PostgreSQL database management frontend
  • PhpWiki
  • - Wiki software
  • PostNuke
  • - Content management system
  • PmWiki
  • - Wiki software
  • PunBB
  • - Internet forum software
  • Smarty
  • - Template engine
  • Typo3
  • - Content management system
  • UBB.threads
  • - Internet forum software
  • vBulletin
  • - Internet forum software
  • WordPress
  • - Content management system
  • Xaraya
  • - Content management system
  • XOOPS
  • - Content management system
    PHP

    PHP homepage

    Advocacy

    Frameworks

    • Achievo ATK - Achievo ATK is an open source framework that allows a programmer to focus on business logic rather than HTML coding.
    • Ampoliros - Advanced distributed PHP web applications platform, featuring an XML-RPC and SOAP interface.
    • Carthag - A PHP 5 native platform for developing enterprise level applications, with J2SE-like API.
    • Copix Framework - Developer-oriented open-source PHP framework offering a 5-layer application architecture. Productivity tools, modular architecture, DAO abstraction layer, plugins, xml configuration, ready-made and customizable CMS modules and more (site & documentation in French).
    • CreaLabs MerlinWork - Fully OO PHP framework for building stateful web applications.
    • DotPHP - Framework that wants to be like ASP.NET.
    • Eocene - Simple OO web development framework for PHP and ASP.NET.
    • Fusebox for PHP
    • iConnect - iConnect is a J2EE like architecture based on the Carthag platform, featuring an EJB-like enterprise application server, a servlets-like web application server and a portlet-like portal server engine.
    • Japha - Implementation of the Java API in PHP 5.
    • LIMB - LIMB is an OpenSource MVC (Model-view-controller) CMF with tree/OO based controller.
    • LogiCreate - PHP web application server and framework.
    • Mojavi Project - Open-source MVC (Model-view-controller) framework for PHP.
    • PEAR - Open-source framework and distribution system for reusable PHP components.
    • phpBeans - Enterprise PHP Object Server, enables RMI in PHP.
    • phpHtmlLib - Set of PHP classes and library functions to help facilitate building, debugging, and rendering of XML, HTML, XHTML, WAP/WML Documents, and SVG (Scalable Vector Graphics) images as well as complex html 'widgets'.
    • php.MVC - Open source framework for PHP web applications that implements the MVC design pattern.
    • Phrame - Web development platform that encourages application architectures based on the "Model2" approach.
    • Plankton Web Application Framework - Integrated collection of classes that provide the building blocks and underlying architecture for an entire PHP web application.
    • PRADO - an event-driven and component-based framework for PHP 5, Grand prize winner project of Zend PHP 5 Coding Contest.
    • Roadsend SiteManager - Open source web application framework for PHP Developers that includes a framework for code modules and layout templates, database connectivity, SmartForms, sessions, and other tools.
    • Smarty - An open source template/presentation framework for abstracting business and presentation logic in a PHP application.
    • Web Application Component Toolkit - Modular patterns based framework for creating high performance web applications.
    • Yawp - Yet Another Web Programming foundation for PHP applications.

    Integrated development environments and debuggers

    IDEs

    Debuggers (all have profiling features)

    Security

    Tutorials and articles

    Miscellaneous

    Template:Major programming languages small