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]*$.

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

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]; } ?>

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

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

include_once

(PHP 4, PHP 5, PHP 7) Examples: include_once with a case insensitive OS in PHP 4 With PHP 4, _once functionality differs with case-insensitive operating systems (like Windows) so for example: <?php include_once "a.php"; // this will include a.php include_once "A.php"; // this will include a.php again! (PHP 4 only) ?>

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

require_once

(PHP 4, PHP 5, PHP 7)

include

(PHP 4, PHP 5, PHP 7) Examples: Basic include example The include statement includes and evaluates the specified file. The documentation below also applies to require. Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the curr