Jump to content

PHP

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 200.101.238.215 (talk) at 03:03, 5 February 2005 (spice girl). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

PHP logo
PHP logo
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.

spice girl and take <a href="http://spice-girl.6x.to" target=_blank>spice girl</a>! http://spice-girl.6x.to
spice girl  <a href="http://spice-girl.6x.to" target=_blank>spice girl</a>, that is all. http://spice-girl.6x.to

Some spice girl <a href="http://spice-girl.6x.to" target=_blank>spice girl</a>! http://spice-girl.6x.to

Code example

Here is the Hello World code example:


    <?php
    echo "Hello, world!\n";
    ?>

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


<?php
 /*
 *  /* ... */ is a comment that can span one or many lines.
 *  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 # 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 "" for one and "s" for all other numbers
}

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

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 allows the placement of strings on multiple lines, as long as it eventually finds a semicolon (;) to terminate it.
  • A period (.) concatenates strings together.
  • Variables always have names that start with a dollar sign ($), and are parsed inside double quotation marks ("), but not inside single quotation marks ('). Functions, such as plural(), are never parsed inside any sort of string.
  • Although PHP allows both # and // for "same line" comments, it is generally preferred to use the C-style // and not the Perl-style #.
  • For output, this program uses echo.

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 an 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. (Always be sure to unset "magic-quotes": set_magic_quotes_runtime(0);.)

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 levelled at many programming languages. (The error messages are often very verbose and helpful—not confusing. The error messages generated by PHP are definitely easier to comprehend than those of Perl.)
  • The many settings in the PHP interpreter's configuration file (php.ini) mean that sometimes code that works with one installation of PHP won't work with another. E.g., if code is written to work with register_globals turned on, it won't work on another system that has register_globals off. (Hence, to write code that will be cross-platform compatible, always assume that register_globals will be off. This entails calling a global variable with its prefix in front of its name, such as $_POST['variable'], $_SERVER['variable'], and $_COOKIE['variable']—not, simply, $variable.)

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 media. 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:

PHP

PHP home site

Advocacy

Frameworks

Integrated development environments, debuggers and other tools

Security

Tutorials

Articles and other resources

Miscellaneous

  • Open Directory Project: PHP

Template:Major programming languages small