You are here

syntax.html in Advanced Help 8

Same filename and directory in other branches
  1. 5 help_example/help/syntax.html
  2. 6 help_example/help/syntax.html

File

help_example/help/syntax.html
View source
<h2> <span class="mw-headline">Syntax</span></h2>
<div class="help-right">
<div class="thumbinner" style="width:182px;"><a target="_blank" href="http://en.wikipedia.org/wiki/Image:PHP_Hello_World_screenshot.png" class="image" title="Syntax-highlighted PHP code"><img alt="Syntax-highlighted PHP code" src="path:180px-PHP_Hello_World_screenshot.png" width="180" height="87" border="0" class="thumbimage" /></a>
<div class="thumbcaption">

<div class="magnify"><a target="_blank" href="http://en.wikipedia.org/wiki/Image:PHP_Hello_World_screenshot.png" class="internal" title="Enlarge"><img src="/skins-1.5/common/images/magnify-clip.png" width="15" height="11" alt="" /></a></div>
<a target="_blank" href="http://en.wikipedia.org/wiki/Syntax-highlighted" class="mw-redirect" title="Syntax-highlighted">Syntax-highlighted</a> PHP code</div>
</div>
</div>
<p>PHP only parses code within its <a target="_blank" href="http://en.wikipedia.org/wiki/Delimiter" title="Delimiter">delimiters</a>. Anything outside its delimiters is sent directly to the output and is not parsed by PHP. The most common delimiters are <span s>&lt;?php</span> and <span>?&gt;</span>, which are open and close delimiters respectively. <span>&lt;script language="php"&gt;</span> and <span>&lt;/script&gt;</span> delimiters are also available. Short tags (<span>&lt;?</span> or <span>&lt;?=</span> and <span>?&gt;</span>) are also commonly used, but like ASP-style tags (<span>&lt;%</span> or <span>&lt;%=</span> and <span>%&gt;</span>), they are 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.<sup id="cite_ref-basic_syntax_28-0" class="reference"><a href="#cite_note-basic_syntax-28" title="">[29]</a></sup> The purpose of these delimiters is to separate PHP code from non-PHP code, including HTML. Everything outside the delimiters is ignored by the parser and is passed through as output.<sup id="cite_ref-29" class="reference"><a href="#cite_note-29" title="">[30]</a></sup></p>

<p>Variables are prefixed with a <a target="_blank" href="http://en.wikipedia.org/wiki/Dollar_sign" title="Dollar sign">dollar symbol</a> and a <a target="_blank" href="http://en.wikipedia.org/wiki/Primitive_type" title="Primitive type">type</a> does not need to be specified in advance. Unlike function and class names, variable names are case sensitive. Both double-quoted (<span>""</span>) and <a target="_blank" href="http://en.wikipedia.org/wiki/Heredoc" class="mw-redirect" title="Heredoc">heredoc</a> strings allow the ability to embed the variable's value into the string.<sup id="cite_ref-30" class="reference"><a href="#cite_note-30" title="">[31]</a></sup> PHP treats <a target="_blank" href="http://en.wikipedia.org/wiki/Newline" title="Newline">newlines</a> as <a target="_blank" href="http://en.wikipedia.org/wiki/Whitespace_%28computer_science%29" title="Whitespace (computer science)">whitespace</a> in the manner of a <a target="_blank" href="http://en.wikipedia.org/wiki/Free-form_language" title="Free-form language">free-form language</a> (except when inside string quotes), and statements are terminated by a semicolon.<sup id="cite_ref-31" class="reference"><a href="#cite_note-31" title="">[32]</a></sup> PHP has three types of <a target="_blank" href="http://en.wikipedia.org/wiki/Comparison_of_programming_languages_%28syntax%29#Comments" title="Comparison of programming languages (syntax)">comment syntax</a>: <span>/* */</span> serves as block comments, and <span>//</span> as well as <span>#</span> are used for inline comments.<sup id="cite_ref-32" class="reference"><a href="#cite_note-32" title="">[33]</a></sup> To output text to the browser, either the <tt>print</tt> function or the <tt>echo</tt> function is used. Both functions are nearly identical; the major difference is that <tt>print</tt> is slower than <tt>echo</tt> because the former will return a status indicating if it was successful or not, whereas the latter does not return a status and only returns the text for output.<sup id="cite_ref-33" class="reference"><a href="#cite_note-33" title="">[34]</a></sup></p>

<p><a name="Data_types" id="Data_types"></a></p>
<h3><span class="mw-headline">Data types</span></h3>
<p>PHP stores whole numbers in a platform-dependent range. This range is typically that of 32-bit <a target="_blank" href="http://en.wikipedia.org/wiki/Signed_number_representations" title="Signed number representations">signed</a> <a target="_blank" href="http://en.wikipedia.org/wiki/Integer_%28computer_science%29" title="Integer (computer science)">integers</a>. Unsigned integers are converted to signed values in certain situations; this behavior is different from other programming languages.<sup id="cite_ref-34" class="reference"><a href="#cite_note-34" title="">[35]</a></sup> Integer variables can be assigned using decimal (positive and negative), <a target="_blank" href="http://en.wikipedia.org/wiki/Octal" title="Octal">octal</a>, and <a target="_blank" href="http://en.wikipedia.org/wiki/Hexadecimal" title="Hexadecimal">hexadecimal</a> notations. <a target="_blank" href="http://en.wikipedia.org/wiki/Real_numbers" class="mw-redirect" title="Real numbers">Real numbers</a> are also stored in a platform-specific range. They can be specified using <a target="_blank" href="http://en.wikipedia.org/wiki/Floating_point" title="Floating point">floating point</a> notation, or two forms of <a target="_blank" href="http://en.wikipedia.org/wiki/Scientific_notation" title="Scientific notation">scientific notation</a>.<sup id="cite_ref-types_35-0" class="reference"><a href="#cite_note-types-35" title="">[36]</a></sup> PHP has a native <a target="_blank" href="http://en.wikipedia.org/wiki/Boolean_datatype" title="Boolean datatype">Boolean</a> type that is similar to the native Boolean types in <a target="_blank" href="http://en.wikipedia.org/wiki/Java_%28programming_language%29" title="Java (programming language)">Java</a> and <a target="_blank" href="http://en.wikipedia.org/wiki/C%2B%2B" title="C++">C++</a>. Using the Boolean type conversion rules, non-zero values are interpreted as true and zero as false, as in Perl and C++.<sup id="cite_ref-types_35-1" class="reference"><a href="#cite_note-types-35" title="">[36]</a></sup> The null data type represents a variable that has no value. The only value in the null data type is <i>NULL</i>.<sup id="cite_ref-types_35-2" class="reference"><a href="#cite_note-types-35" title="">[36]</a></sup> 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.<sup id="cite_ref-types_35-3" class="reference"><a href="#cite_note-types-35" title="">[36]</a></sup> 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 <a target="_blank" href="http://en.wikipedia.org/wiki/Hash_table" title="Hash table">hashes</a> with both keys and values, and the two can be intermingled.<sup id="cite_ref-types_35-4" class="reference"><a href="#cite_note-types-35" title="">[36]</a></sup> PHP also supports <a target="_blank" href="http://en.wikipedia.org/wiki/String_%28computing%29" class="mw-redirect" title="String (computing)">strings</a>, which can be used with single quotes, double quotes, or <a target="_blank" href="http://en.wikipedia.org/wiki/Heredoc" class="mw-redirect" title="Heredoc">heredoc syntax</a>.<sup id="cite_ref-36" class="reference"><a href="#cite_note-36" title="">[37]</a></sup></p>

<p><a name="Functions" id="Functions"></a></p>
<h3><span class="mw-headline">Functions</span></h3>
<p>PHP has hundreds of base functions and thousands more from extensions. Functions are not <a target="_blank" href="http://en.wikipedia.org/wiki/First-class_function" title="First-class function">first-class functions</a> and can only be referenced by their name. <sup id="cite_ref-functions_37-0" class="reference"><a href="#cite_note-functions-37" title="">[38]</a></sup> User-defined functions can be created at any time without being prototyped.<sup id="cite_ref-functions_37-1" class="reference"><a href="#cite_note-functions-37" title="">[38]</a></sup> Functions can be defined inside code blocks, permitting a <a target="_blank" href="http://en.wikipedia.org/wiki/Dynamic_dispatch" title="Dynamic dispatch">run-time decision</a> as to whether or not a function should be defined. Function calls must use parentheses, with the exception of zero argument class <a target="_blank" href="http://en.wikipedia.org/wiki/Constructor_%28computer_science%29" title="Constructor (computer science)">constructor</a> functions called with the PHP <span>new</span> operator, where parentheses are optional. PHP supports quasi-<a target="_blank" href="http://en.wikipedia.org/wiki/Anonymous_function" title="Anonymous function">anonymous functions</a> through the <span>create_function()</span> function, although they are not true anonymous functions because anonymous functions are nameless, but functions can only be referenced by name, or indirectly through a variable <span>$function_name();</span>, in PHP.<sup id="cite_ref-functions_37-2" class="reference"><a href="#cite_note-functions-37" title="">[38]</a></sup></p>

<p><a name="Objects" id="Objects"></a></p>
<h3><span class="mw-headline">Objects</span></h3>
<p>Basic <a target="_blank" href="http://en.wikipedia.org/wiki/Object-oriented_programming" title="Object-oriented programming">object-oriented programming</a> functionality was added in PHP 3.<sup id="cite_ref-history_2-10" class="reference"><a href="#cite_note-history-2" title="">[3]</a></sup> Object handling was completely rewritten for PHP 5, expanding the feature set and enhancing performance.<sup id="cite_ref-php_5_objects_38-0" class="reference"><a href="#cite_note-php_5_objects-38" title="">[39]</a></sup> In previous versions of PHP, objects were handled like <a target="_blank" href="http://en.wikipedia.org/wiki/Primitive_type" title="Primitive type">primitive types</a>.<sup id="cite_ref-php_5_objects_38-1" class="reference"><a href="#cite_note-php_5_objects-38" title="">[39]</a></sup> 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 <a target="_blank" href="http://en.wikipedia.org/wiki/Smart_pointer#Handles" title="Smart pointer">handle</a>, and not by value. PHP 5 introduced private and protected <a target="_blank" href="http://en.wikipedia.org/wiki/Member_variable" class="mw-redirect" title="Member variable">member variables</a> and methods, along with <a target="_blank" href="http://en.wikipedia.org/wiki/Abstract_type" title="Abstract type">abstract classes</a> and <a target="_blank" href="http://en.wikipedia.org/wiki/Final_type" class="mw-redirect" title="Final type">final classes</a> as well as <a target="_blank" href="http://en.wikipedia.org/wiki/Abstract_method" class="mw-redirect" title="Abstract method">abstract methods</a> and <a target="_blank" href="http://en.wikipedia.org/wiki/Final_method" class="mw-redirect" title="Final method">final methods</a>. It also introduced a standard way of declaring <a target="_blank" href="http://en.wikipedia.org/wiki/Constructor_%28computer_science%29" title="Constructor (computer science)">constructors</a> and <a target="_blank" href="http://en.wikipedia.org/wiki/Destructor_%28computer_science%29" title="Destructor (computer science)">destructors</a>, similar to that of other object-oriented languages such as <a target="_blank" href="http://en.wikipedia.org/wiki/C%2B%2B" title="C++">C++</a>, and a standard <a target="_blank" href="http://en.wikipedia.org/wiki/Exception_handling" title="Exception handling">exception handling</a> model. Furthermore, PHP 5 added <a target="_blank" href="http://en.wikipedia.org/wiki/Interfaces" class="mw-redirect" title="Interfaces">interfaces</a> and allowed for multiple interfaces to be implemented. There are special interfaces that allow objects to interact with the runtime system. <a target="_blank" href="http://en.wikipedia.org/wiki/Object" title="Object">Objects</a> implementing <a target="_blank" href="http://en.wikipedia.org/wiki/ArrayAccess" class="mw-redirect" title="ArrayAccess">ArrayAccess</a> can be used with array syntax and <a target="_blank" href="http://en.wikipedia.org/wiki/Object" title="Object">objects</a> implementing <a target="_blank" href="http://en.wikipedia.org/wiki/Iterator" title="Iterator">Iterator</a> or <a target="_blank" href="http://en.wikipedia.org/wiki/IteratorAggregate" class="mw-redirect" title="IteratorAggregate">IteratorAggregate</a> can be used with the <span>foreach</span> language construct. There is no <a target="_blank" href="http://en.wikipedia.org/wiki/Virtual_table" class="mw-redirect" title="Virtual table">virtual table</a> feature in the engine, so <a target="_blank" href="http://en.wikipedia.org/wiki/Static_variable" title="Static variable">static variables</a> are bound with a name instead of a reference at compile time.<sup id="cite_ref-zend_engine_2_39-0" class="reference"><a href="#cite_note-zend_engine_2-39" title="">[40]</a></sup></p>

<p>If the developer creates a copy of an object using the reserved word <i>clone</i>, the Zend engine will check if a <tt>__clone()</tt> method has been defined or not. If not, it will call a default <tt>__clone()</tt> which will copy the object's properties. If a <tt>__clone()</tt> 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 <a href="http://en.wiktionary.org/wiki/replica" class="extiw" title="wikt:replica">replica</a> of the source object and only override properties that need to be changed.<sup id="cite_ref-40" class="reference"><a href="#cite_note-40" title="">[41]</a></sup></p>

<p><a name="Resources" id="Resources"></a></p>
<h2> <span class="mw-headline">Resources</span></h2>
<p>PHP includes <a target="_blank" href="http://en.wikipedia.org/wiki/List_of_PHP_libraries" title="List of PHP libraries">free and open source libraries</a> with the core build. PHP is a fundamentally <a target="_blank" href="http://en.wikipedia.org/wiki/Internet" title="Internet">Internet</a>-aware system with modules built in for accessing <a target="_blank" href="http://en.wikipedia.org/wiki/File_transfer_protocol" class="mw-redirect" title="File transfer protocol">FTP</a> servers, many database servers, embedded SQL libraries such as embedded <a target="_blank" href="http://en.wikipedia.org/wiki/MySQL" title="MySQL">MySQL</a> and <a target="_blank" href="http://en.wikipedia.org/wiki/SQLite" title="SQLite">SQLite</a>, <a target="_blank" href="http://en.wikipedia.org/wiki/Lightweight_Directory_Access_Protocol" title="Lightweight Directory Access Protocol">LDAP</a> servers, and others. Many functions familiar to C programmers such as those in the <tt><a target="_blank" href="http://en.wikipedia.org/wiki/Stdio.h" title="Stdio.h">stdio</a></tt> family are available in the standard PHP build.<sup id="cite_ref-41" class="reference"><a href="#cite_note-41" title="">[42]</a></sup> PHP has traditionally used features such as "<a target="_blank" href="http://en.wikipedia.org/wiki/Magic_quotes" title="Magic quotes">magic_quotes_gpc</a>" and "magic_quotes_runtime" which attempt to escape apostrophes (') and quotes (") in strings in the assumption that they will be used in databases, to prevent <a target="_blank" href="http://en.wikipedia.org/wiki/SQL_injection" title="SQL injection">SQL injection</a> attacks. This leads to confusion over which data is escaped and which is not, and to problems when data is not in fact used as input to a database and when the escaping used is not completely correct.<sup id="cite_ref-42" class="reference"><a href="#cite_note-42" title="">[43]</a></sup> To make code portable between servers which do and do not use magic quotes, developers can preface their code with a script to reverse the effect of magic quotes when it is applied.<sup id="cite_ref-43" class="reference"><a href="#cite_note-43" title="">[44]</a></sup></p>

<p>PHP allows developers to write <a target="_blank" href="http://en.wikipedia.org/wiki/Extension_%28computing%29" class="mw-redirect" title="Extension (computing)">extensions</a> in <a target="_blank" href="http://en.wikipedia.org/wiki/C_%28programming_language%29" title="C (programming language)">C</a> to add functionality to the PHP language. These can then be compiled into PHP or loaded dynamically at runtime. Extensions have been written to add support for the <a target="_blank" href="http://en.wikipedia.org/wiki/Windows_API" title="Windows API">Windows API</a>, process management on <a target="_blank" href="http://en.wikipedia.org/wiki/Unix-like" title="Unix-like">Unix-like</a> <a target="_blank" href="http://en.wikipedia.org/wiki/Operating_system" title="Operating system">operating systems</a>, multibyte strings (<a target="_blank" href="http://en.wikipedia.org/wiki/Unispan" title="Unispan">Unispan</a>), <a target="_blank" href="http://en.wikipedia.org/wiki/CURL" title="CURL">cURL</a>, and several popular <a target="_blank" href="http://en.wikipedia.org/wiki/Compression_formats" class="mw-redirect" title="Compression formats">compression formats</a>. Some more unusual features include integration with <a target="_blank" href="http://en.wikipedia.org/wiki/Internet_relay_chat" class="mw-redirect" title="Internet relay chat">Internet relay chat</a>, dynamic generation of images and <a target="_blank" href="http://en.wikipedia.org/wiki/Adobe_Flash" title="Adobe Flash">Adobe Flash</a> content, and even <a target="_blank" href="http://en.wikipedia.org/wiki/Speech_synthesis" title="Speech synthesis">speech synthesis</a>. The <a target="_blank" href="http://en.wikipedia.org/wiki/PHP_Extension_Community_Library" class="mw-redirect" title="PHP Extension Community Library">PHP Extension Community Library</a> (PECL) project is a repository for extensions to the PHP language.<sup id="cite_ref-44" class="reference"><a href="#cite_note-44" title="">[45]</a></sup></p>

<p>As with many scripting languages, PHP scripts are normally kept as human-readable source code, even on production web servers.<sup id="cite_ref-45" class="reference"><a href="#cite_note-45" title="">[46]</a></sup> While this allows flexibility, releasing scripts in source form is undesirable for commercial software developers, and can raise issues with security of web servers; as an example, if a hacker acquires control of a server, database passwords may be quickly discovered, and undesirable changes to scripts may be made that remain undiscovered indefinitely. Various encoding tools are available for PHP to offer code protection.<sup class="noprint Template-Fact"><span title="This claim needs references to reliable sources&#160;since March 2008" style="white-space: nowrap;">[<i><a target="_blank" href="http://en.wikipedia.org/wiki/Wikipedia:Citation_needed" title="Wikipedia:Citation needed">citation needed</a></i>]</span></sup></p>
<p>span optimizers improve the quality of the compiled code by reducing its size and making changes that can reduce the execution time and improve performance. The nature of the PHP <a target="_blank" href="http://en.wikipedia.org/wiki/Compiler" title="Compiler">compiler</a> is such that there are often opportunities for <a target="_blank" href="http://en.wikipedia.org/wiki/Optimization_%28computer_science%29" title="Optimization (computer science)">span optimization</a><sup id="cite_ref-46" class="reference"><a href="#cite_note-46" title="">[47]</a></sup>, and an example of a code optimizer is the <a target="_blank" href="http://en.wikipedia.org/wiki/PHP_accelerator#Zend_Optimizer" title="PHP accelerator">Zend Optimizer</a> PHP extension.<sup id="cite_ref-47" class="reference"><a href="#cite_note-47" title="">[48]</a></sup></p>

<p><a target="_blank" href="http://en.wikipedia.org/wiki/PHP_accelerator" title="PHP accelerator">PHP accelerators</a> can offer significant performance gains by <a target="_blank" href="http://en.wikipedia.org/wiki/Caching" class="mw-redirect" title="Caching">caching</a> the compiled form of a PHP script in <a target="_blank" href="http://en.wikipedia.org/wiki/Shared_memory" title="Shared memory">shared memory</a> to avoid the overhead of <a target="_blank" href="http://en.wikipedia.org/wiki/Parsing" title="Parsing">parsing</a> and <a target="_blank" href="http://en.wikipedia.org/wiki/Compiling" class="mw-redirect" title="Compiling">compiling</a> the code every time the script runs. They may also perform <a target="_blank" href="http://en.wikipedia.org/wiki/span_optimization" class="mw-redirect" title="span optimization">span optimization</a> to provide increased execution performance.<sup class="noprint Template-Fact"><span title="This claim needs references to reliable sources&#160;since March 2008" style="white-space: nowrap;">[<i><a target="_blank" href="http://en.wikipedia.org/wiki/Wikipedia:Citation_needed" title="Wikipedia:Citation needed">citation needed</a></i>]</span></sup></p>