$_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

$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

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

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

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

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;                        /

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

Using namespaces: fallback to global function/constant

(PHP 5 >= 5.3.0, PHP 7) Examples: Accessing global classes inside a namespace Inside a namespace, when PHP encounters an unqualified Name in a class name, function or constant context, it resolves these with different priorities. Class names always resolve to the current namespace name. Thus to access internal or non-namespaced user classes, one must refer to them with their fully quali

Global space

(PHP 5 >= 5.3.0, PHP 7) Examples: Using global space specification Without any namespace definition, all class and function definitions are placed into the global space - as it was in PHP before namespaces were supported. Prefixing a name with \ will specify that the name is required from the global space even in the context of the namespace. <?php namespace A\B\C; /* This functi

Using namespaces: Aliasing/Importing

(PHP 5 >= 5.3.0, PHP 7) Examples: importing/aliasing with the use operator The ability to refer to an external fully qualified name with an alias, or importing, is an important feature of namespaces. This is similar to the ability of unix-based filesystems to create symbolic links to a file or to a directory. All versions of PHP that support namespaces support three kinds of aliasing