Smallwikipedialogo.png Wikipedia has an article related to:

A PHP page is a page containing PHP scripts, used to display web pages over the internet.

PHP is a widely-used general-purpose scripting language that is especially suited for web development and can be embedded into HTML. It generally runs on a web server, which is configured to take PHP code as input and create web page content as output. It can be deployed on most web servers and on almost every operating system and platform free of charge.[1] PHP is installed on more than 20 million websites and 1 million web servers.[2]

Syntax and semanticsEdit

The syntax of the PHP programming language is the set of rules that defines how a PHP program will be written and interpreted.

Overview Edit

PHP only parses code within its delimiters. Anything outside its delimiters is sent directly to the output and not parsed by PHP. The most common delimiters are <?php and ?>, respectively open and close delimiters.

One of the language characteristic features is implicit variable declaration. Variables are prefixed with a dollar sign 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 a variable's value into the string.[3] PHP treats newlines as whitespace, in the manner of a free-form language (except when inside string quotes). Statements are terminated by a semicolon.[4] PHP has three types of comment syntax: /* */ which serves as block comments, and // as well as # which are used for inline comments.[5] 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 does not return a status and only returns the text for output.[6]

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

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"

Data typesEdit

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.[8] PHP has a native Boolean type, named "boolean". Using the Boolean type conversion rules, non-zero values are interpreted as true and zero as false.[8] The null data type represents a variable that has no value. The only value in the null data type is NULL.[8] 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.[8] 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 syntactically be used as arrays.[8]


PHP has hundreds of base functions and thousands more from extensions. Functions are not first-class functions and can only be referenced by their name. [9] User-defined functions can be created at any time and without being prototyped.[9] 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. Function calls must use parentheses with the exception of zero argument class constructor functions called with the PHP new operator, where parentheses are optional.

An example function definition is the following:

function hello()
 echo "Hello World!\n";

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

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:

function hello()
 return 'Hello';
function world()
 return "World!\n";
$fn1 = 'hello';
$fn2 = 'world';
echo $fn1() . ' ' . $fn2();


Basic object-oriented programming functionality was added in PHP 3.[10] Object handling was completely rewritten for PHP 5, expanding the feature set and enhancing performance.[11] In previous versions of PHP, objects were handled like primitive types.[11] 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 destructor, similar to that of other object-oriented languages, 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. Object]s 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.[12]

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


External linksEdit

This page uses CC-BY-SA content from Wikipedia (authors). Smallwikipedialogo.png

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.