PHP syntax and semantics

PHP syntax and semantics

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

Overview

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 and ?>, respectively open and close delimiters. 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.cite web|title=PHP: Basic syntax|url=http://www.php.net/manual/en/language.basic-syntax.php|publisher=The PHP Group|accessdate=2008-02-22] The purpose of these delimiters is to separate PHP code from non-PHP code (notably HTML). Everything outside the delimiters is ignored by the parser and is passed through as output. [cite web|accessdate=2008-02-25|url=http://ca3.php.net/manual/en/tutorial.firstpage.php|title=Your first PHP-enabled page |publisher=The PHP Group ]

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 a variable's value into the string. [cite web|accessdate=2008-03-16|url=http://www.php.net/manual/en/language.variables.php|title=Variables |publisher=The PHP Group ] PHP treats newlines as whitespace, in the manner of a free-form language (except when inside string quotes). Statements are terminated by a semicolon. [cite web|accessdate=2008-03-16|url=http://www.php.net/basic-syntax.instruction-separation|title=Instruction separation |publisher=The PHP Group ] PHP has three types of comment syntax: /* */ which serves as block comments, and // as well as # which are used for inline comments. [cite web|accessdate=2008-03-16|url=http://ca3.php.net/manual/en/language.basic-syntax.comments.php|title=Comments |publisher=The PHP Group ] 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. [cite web|accessdate=2008-03-16|url=http://www.php.net/print|title=print |publisher=The PHP Group ]

The usual Hello World code example for PHP is: [cite web|accessdate=2008-02-25|url=http://php.codenewbie.com/articles/php/1485/Hello_World-Page_1.html|title=Hello World |publisher=Code Newbie ]

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:

<?=$page_title;?>

Hello

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.cite web|accessdate=2008-03-16|url=http://www.php.net/manual/en/language.types.php|title=Types |publisher=The PHP Group ] 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. 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 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 thousands more from extensions. Functions are not first-class functions and can only be referenced by their name.cite web|accessdate=2008-03-16|url=http://www.php.net/manual/en/language.functions.php|title=Functions |publisher=The PHP Group ] User-defined functions can be created at any time and without being prototyped. 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:

hello();?>

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:$fn1 = 'hello';$fn2 = 'world';

echo $fn1() . ' ' . $fn2();

?>

Objects

Basic object-oriented programming functionality was added in PHP 3.cite web|accessdate=2008-02-25|url=http://www.php.net/history|title=History of PHP and related projects |publisher=The PHP Group ] 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.cite web|accessdate=2008-03-16|url=http://mjtsai.com/blog/2004/07/15/php-5-object-references/|title=PHP 5 Object References |publisher=mjtsai ] 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. [cite web|accessdate=2008-03-16|url=http://ca3.php.net/zend-engine-2.php|title=Classes and Objects (PHP 5) |publisher=The PHP Group ]

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 of the source object and only override properties that need to be changed. [cite web|accessdate=2008-03-16|url=http://ca3.php.net/language.oop5.cloning|title=Object cloning |publisher=The PHP Group ]

References


Wikimedia Foundation. 2010.

Look at other dictionaries:

  • PHP — This article is about the scripting language. For other uses, see PHP (disambiguation). PHP PHP: Hypertext Preprocessor Paradigm(s) imperative, object oriented, Procedural, reflective Appeared in …   Wikipedia

  • Operators in C and C++ — This is a list of operators in the C and C++ programming languages. All the operators listed exist in C++; the fourth column Included in C , dictates whether an operator is also present in C. Note that C does not support operator overloading.… …   Wikipedia

  • Comparison of Java and C Sharp — This is a comparison of the C# programming language with the Java programming language. As the two are both garbage collected runtime compiled languages with syntax derived from C and C++, there are many similarities between Java and C#. However …   Wikipedia

  • Yes and no — For other uses, see Yes and no (disambiguation). Yes and no are two words for expressing affirmatives and negatives respectively in English (e.g. Are you hungry? Yes, I am. ). Early Middle English had a four form system, but Modern English has… …   Wikipedia

  • Java syntax — The syntax of the Java programming language is a set of rules that defines how a Java program is written and interpreted. Data structuresAlthough the language has special syntax for them, arrays and strings are not primitive types: they are… …   Wikipedia

  • Comparison of C Sharp and Visual Basic .NET — The correct title of this article is Comparison of C# and Visual Basic .NET. The substitution or omission of the # sign is because of technical restrictions. Programming language comparisons General comparison Basic syntax Basic instructions …   Wikipedia

  • List of computing and IT abbreviations — This is a list of computing and IT acronyms and abbreviations. Contents: 0–9 A B C D E F G H I J K L M N O P Q R S T U V W X Y …   Wikipedia

  • Exception handling syntax — varies between programming languages to accommodate their overall syntax. Some languages don t call the concept exception handling or they may not have direct facilities for it, but they can still provide means for implementing it. Catalogue of… …   Wikipedia

  • Futures and promises — In computer science, future, promise, and delay refer to constructs used for synchronization in some concurrent programming languages. They describe an object that acts as a proxy for a result that is initially not known, usually because the… …   Wikipedia

  • Multimodal Architecture and Interfaces — is an open standard developed by the World Wide Consortium since 2005. Currently it is a working draft ( Working Draft ) of the W3C . The document is a technical report specifying a multimodal system architecture and its generic interfaces to… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”