goto

(PHP 5 >= 5.3.0, PHP 7) Examples: goto example The goto operator can be used to jump to another section in the program. The target point is specified by a label followed by a colon, and the instruction is given as goto followed by the desired target label. This is not a full unrestricted goto. The target label must be within the same file and context, meaning that you cannot jump out of

User-defined functions

Examples: Pseudo code to demonstrate function uses A function may be defined using syntax such as the following: <?php function foo($arg_1, $arg_2, /* ..., */ $arg_n) {     echo "Example function.\n";     return $retval; } ?> Conditional functions Functions need not be defined before they are referenced, except when a function is conditionally defined as shown in the two examples below. When a function is defined in a conditional m

Function arguments

Examples: Passing arrays to functions Information may be passed to functions via the argument list, which is a comma-delimited list of expressions. The arguments are evaluated from left to right. PHP supports passing arguments by value (the default), passing by reference, and default argument values. Variable-length argument lists are also supported. <?php function takes_array($input) {     echo "$input[0] + $input[1] = ", $input[0]+$input[1]; } ?>

Returning values

Examples: Use of return <?php function square($num) {     return $num * $num; } echo square(4);   // outputs '16'. ?> Returning an array to get multiple values A function can not return multiple values, but similar results can be obtained by returning an array. <?php function small_numbers() {     return array (0, 1, 2); } list ($zero, $one, $two) = small_numbers(); ?> Returning a reference from a function

Variable functions

Examples: Variable function example PHP supports the concept of variable functions. This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it. Among other things, this can be used to implement callbacks, function tables, and so forth. Variable functions won't work with language constructs such as echo, print, unset(), isset(), empty(), includ

Internal (built-in) functions

Anonymous functions

Examples: Anonymous function example Anonymous functions, also known as closures, allow the creation of functions which have no specified name. They are most useful as the value of callback parameters, but they have many other uses. <?php echo preg_replace_callback('~-([a-z])~', function ($match) {     return strtoupper($match[1]); }, 'hello-world'); // outputs helloWorld ?> Anonymous function variable assignment example Closures can

Basics

Basic class definitions begin with the keyword class, followed by a class name, followed by a pair of curly braces which enclose the definitions of the properties and methods belonging to the class. The class name can be any valid label, provided it is not a PHP reserved word. A valid class name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: ^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$.

Properties

Examples: property declarations Class member variables are called "properties". You may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able

Class Constants

Examples: Defining and using a constant It is possible to define constant values on a per-class basis remaining the same and unchangeable. Constants differ from normal variables in that you don't use the $ symbol to declare or use them. The value must be a constant expression, not (for example) a variable, a property, or a function call. It's also possible for interfaces to have constants. Look at the interface documentation for examples. As of PHP 5.3.0, it's po