Advertisement

PHP

From Academic Kids

Template:Dablink

Missing image
Php-med-trans.png
PHP logo

PHP is a popular open-source programming language used primarily for developing server-side applications and dynamic web content, and more recently, other software. The name is a recursive acronym for "PHP: Hypertext Preprocessor". This is actually a retronym; see history of PHP.

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.NET/C#/VB.NET system, Macromedia's ColdFusion system, Sun Microsystems' JSP/Java system, and to the CGI/Perl system.

Contents

Overview

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 programming 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 management systems, such as MySQL, Oracle, IBM DB2, Microsoft SQL Server, PostgreSQL and SQLite 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 (http://www.php.net/) contains extensive documentation (http://www.php.net/manual/). 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 the Danish-Canadian programmer Rasmus Lerdorf in 1994 to display his rsum 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 (http://www.zend.com/zend/zend-engine-summary.php) 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.

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 million 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] (http://www.php.net/usage.php)

Due to PHP's popularity, a new breed of programmer has emerged—one who is familiar only 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 adoption as a genuine programming language (that is, 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 such 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 a Hello World code example (See Basic syntax (http://php.net/manual/en/language.basic-syntax.php) in the PHP manual):

<?php
 echo 'Hello World!';
?>

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

<?php
   /*
   *  This block is a comment that can span one or many lines.
   *  This kind of comment does not need stars (*) in the     
   *  beginning of each line, but including them is a common 
   *  practice. // and # are also comments. They only comment 
   *  the text that are after them in the same line. They 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 ?: operator is similar to if-else: 
   // (test_condition ? true : false)
   // In this case it's used to return '' (an empty string) for one 
   // and 's' for all other numbers
}

// We define a variable called $lb to contain an 
// XHTML line break and a newline :
$lb = "<br />\n";

// Be sure to count DOWN
for ($i = 99; $i > 0; $i--)
{
   echo $i . ' bottle' . plural($i) . ' of beer on the wall,' . $lb;
   // We don't actually need a new echo for each line. Let's see:
   echo $i . ' bottle' . plural($i) . ' of beer.' . $lb .
      'Take one down, pass it around,' . $lb .
      ($i - 1 != 0 ? $i - 1 : 'No more') .
      ' bottle' . plural($i - 1) . ' of beer on the wall' . $lb . $lb;
}

echo 'Go to the store,' . $lb . 'buy some more,' . $lb . 
   '99 bottles of beer on the wall!';
?>

Notes:

  • PHP treats new lines as white space, in the manner of a free-form language (except when inside string quotes). A line of code is terminated only by a semicolon (;) except in a few special cases. [2] (http://php.net/manual/en/language.basic-syntax.instruction-separation.php)
  • A period (.) concatenates strings together. [3] (http://php.net/manual/en/language.types.string.php)
  • Variables always have names that start with a dollar sign ($), and are evaluated inside double quotation marks ("), but not inside single quotation marks ('). Functions, such as plural(), and other expressions are not evaluated inside double quotes but can be added to strings using periods for concatenation.

Example:

$var = 'string' . function() . 'rest of string';

[4] (http://php.net/manual/en/language.variables.php)

  • Although PHP allows both # and // for "same line" comments, it is generally preferred to use the C-style // and not the Perl-style #. [5] (http://us2.php.net/manual/en/language.basic-syntax.comments.php)
  • For output, this program uses echo. print and printf may also be used for this purpose.

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 (http://www.php.net/manual/en/))

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 (http://www.zend.com/php5/)):

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 C++: 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 an exception model similar to that of other programming languages.

More additions and examples of the additions mentioned above are available in the Classes and Objects chapter (http://php.net/manual/en/language.oop5.php) of the PHP 5 manual.

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.

Here is an example of creating an object:

<?php
class Car {
   var $miles; //variable that can be accessed outside the class

   function Car($param) { //is called when class "Car" is created
      dosomething($param);
   }

   function startcar() {
      startthecar();
   }

   function stopcar() {
      stopthecar();
   }

   function miles {
      return $this->miles;
   }
}

$car = new Car($param);
echo $car->miles; //echos the value of the property "miles" of the class "Car"
?>

For more on PHP's OOP abilities, see:

Criticisms

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

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; however, an operation on an uninitialized variable does raise an E_NOTICE level error, errors that are hidden by default. This leads to security holes with register_globals (not on by default), as mentioned below. See also error_reporting() (http://php.net/manual/en/function.error-reporting.php).
  • 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). Although all new functions do follow a naming standard, old names remain for backward compatibility reasons.
  • 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 (http://php.net/manual/en/function.strpos.php). This is related to PHP's dynamic typing. A workaround is using strict (===) type checking as opposed to loose (==). See also the manual on type juggling (http://php.net/manual/en/language.types.type-juggling.php).
  • 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. Most functions are not enabled by default, but become available when PHP is linked against the required libraries.
  • 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 or encouraging beginners to write PHP which is vulnerable to SQL_injection when used on a system with it turned off. (Always be sure to check for "magic-quotes": get_magic_quotes_gpc(); and to unset "magic-quotes-runtime": set_magic_quotes_runtime(0);.) Magic Quotes are turned off by default in PHP 5. For more information, see the security section in the Magic Quotes chapter (http://php.net/manual/en/security.magicquotes.php) of the PHP manual.

Security

  • If register_globals (http://www.php.net/register_globals) is enabled in PHP's configuration file, users could cause harm by manipulating poorly written code. As of version 4.2.0 (http://php.net/release_4_2_0.php) register_globals defaults to off. For more information, see the security section in the Using Register Globals chapter (http://php.net/manual/en/security.globals.php) of the PHP manual.
  • 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. See also strip_tags() (http://php.net/manual/en/function.strip-tags.php).
  • 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.
  • PHP has no variable tainting (http://gunther.web66.com/FAQS/taintmode.html) mechanism (which is very useful for a language designed to accept and process untrusted input.)

Miscellaneous

  • Error messages are said to be confusing. This is a common criticism levelled at many programming languages. The error messages generated by PHP are easier to comprehend than those of Perl. [6] (http://www.javascriptkit.com/howto/phpcgi.shtml) For further information, see the manual section on PHP parser tokens (http://php.net/manual/en/tokens.php).
  • The many settings in the PHP interpreter's configuration file (php.ini) mean that code that works with one installation of PHP might not work with another. For example, if code is written to work with register_globals turned on, it won't work on another system that has register_globals turned off. This makes it necessary to write code that is cross-platform compatible by assuming that register_globals will be off and therefore calling a global variable with its prefix in front of its name, such as $_POST['variable'], $_SERVER['variable'] and $_COOKIE['variable']—not, simply, $variable. For more information, see the manual page on using external variables (http://php.net/manual/en/language.variables.external.php).
  • Some PHP extensions use libraries that are not threadsafe, hence rendering with Apache 2's Multithreaded MPM (http://httpd.apache.org/docs-2.0/mpm.html) (multi-processing module) may cause crashes.

Support

PHP has a formal development manual (http://www.php.net/docs.php) that is maintained by the open source community. In addition, answers to most questions can often be found by doing a simple internet search. 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:

Applications built with PHP

The following is a list of notable applications developed using PHP:

External links

Template:Book

PHP home site

Advocacy

Resources

Security

Miscellaneous

  • Open Directory Project: PHP (http://www.dmoz.org/Computers/Programming/Languages/PHP/)

Template:Major programming languages smallar:Php bg:PHP cs:PHP da:PHP de:PHP eo:PHP es:PHP et:PHP fa:پی‌اچ‌پی fi:PHP fr:PHP hypertext preprocessor is:PHP it:PHP he:PHP hu:PHP programozsi nyelv ja:PHP Hypertext Preprocessor ko:PHP ku:PHP lt:PHP lv:PHP nl:Programmeertaal PHP no:PHP pl:PHP pt:PHP ro:PHP ru:PHP sk:PHP sl:PHP sr:PHP sv:PHP th:ภาษาพีเอชพี tr:PHP uk:Мова програмування PHP vi:PHP zh:PHP

Navigation

Academic Kids Menu

  • Art and Cultures
    • Art (http://www.academickids.com/encyclopedia/index.php/Art)
    • Architecture (http://www.academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (http://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (http://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools