Name resolution rules

(PHP 5 >= 5.3.0, PHP 7) Examples: Name resolutions illustrated <?php namespace A; use B\D, C\E as F; // function calls foo();      // first tries to call "foo" defined in namespace "A"             // then calls global function "foo" \foo();     // calls function "foo" defined in global scope my\foo();   // calls function "foo" defined in namespace "A\my" F();        // first tr

Extending Exceptions

Examples: The Built in Exception class A User defined Exception class can be defined by extending the built-in Exception class. The members and properties below, show what is accessible within the child class that derives from the built-in Exception class. <?php class Exception {     protected $message = 'Unknown exception';   // exception message     private   $string;                          // __toString cache     protected $code = 0;                        /

Generators overview

(PHP 5 >= 5.5.0, PHP 7) Examples: Implementing range() as a generator Generators provide an easy way to implement simple iterators without the overhead or complexity of implementing a class that implements the Iterator interface. A generator allows you to write code that uses foreach to iterate over a set of data without needing to build an array in memory, which may cause you to exce

Generator syntax

Examples: A simple example of yielding values The heart of a generator function is the yield keyword. In its simplest form, a yield statement looks much like a return statement, except that instead of stopping execution of the function and returning, yield instead provides a value to the code looping over the generator and pauses execution of the generator function. <?php function gen_one_to_three() {     for ($i = 1; $i <= 3; $i++) {         // Note that $i i

Comparing generators with Iterator objects

Examples: The primary advantage of generators is their simplicity. Much less boilerplate code has to be written compared to implementing an Iterator class, and the code is generally much more readable. For example, the following function and class are equivalent: <?php function getLinesFromFile($fileName) {     if (!$fileHandle = fopen($fileName, 'r')) {         return;     }       while (false !== $line = fgets($fileHandle)) {         yield $line;     }       f

$GLOBALS

(PHP 4, PHP 5, PHP 7) References all variables available in global scope An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array. Notes: This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. The

$_SERVER

(PHP 4 >= 4.1.0, PHP 5, PHP 7) Server and execution environment information $_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here. That said, a large number of these variables are accounted for in the » CGI/1.1 specificati

$_GET

(PHP 4 >= 4.1.0, PHP 5, PHP 7) HTTP GET variables An associative array of variables passed to the current script via the URL parameters. $HTTP_GET_VARS contains the same initial information, but is not a superglobal. (Note that $HTTP_GET_VARS and $_GET are different variables and that PHP handles them as such) Changelog: 4.1.0 Introduced $_GET that deprecated $HTTP_GET_VA

$_POST

(PHP 4 >= 4.1.0, PHP 5, PHP 7) HTTP POST variables An associative array of variables passed to the current script via the HTTP POST method when using application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request. $HTTP_POST_VARS contains the same initial information, but is not a superglobal. (Note that $HTTP_POST_VARS and $_POST are different variables and that PHP handles them as such)

$_FILES

(PHP 4 >= 4.1.0, PHP 5, PHP 7) HTTP File Upload variables An associative array of items uploaded to the current script via the HTTP POST method. $HTTP_POST_FILES contains the same initial information, but is not a superglobal. (Note that $HTTP_POST_FILES and $_FILES are different variables and that PHP handles them as such) Changelog: 4.1.0 Introduced $_FILES that depreca