Any valid PHP code may appear inside a function even other function and class definitions.
The function name must start with any letter or underscore followed by digits, letters, underscores. They are case insensitive.
Funcs need not to be defined before they are referenced, except when a function is conditionally defined.
PHP does not support function overloadind, nor it is possible to undefine or redifine previously-declared functions.
func_num_args() , func_get_arg() , func_get_args()
All functions and classes have the global scope.
It is possible to call recursive functions
function foo(){ function bar(){ echo "I don't exist until foo() is called. \n"; } } function recursion($a){ if($a < 20){ echo "$a\n"; recursion($a+1); } }
Arguments
By default are passed by value(if the value changes inside the function it does not get changed outside of it).
To pass an argument by referance prepend him with an ampersand.function (&arg)
Default arguments may be passed, which must be a constant expression. They have to be on the right side of any non-default arguments.($type="animal")
Type declarations allow functions to require that parameters are of a certain type (PHP 5 recoverable fatal error, PHP 7 TypeError exception). The type name should be added before the parameter name. The declaration can be made to accept NULL values if the default value of the parameter is set to NULL. By default, PHP will coerce values of the wrong type (non-strict mode).
Types PHP 5: Class/Interface, self, array, callable.
Types PHP 7: += bool, float, int, string.
Strict mode is possible to be enabled on a per-file basis declare(strict_types=1);. In strict mode, only a variable of the exact type of the type declaration will be accepted(TypeError). The only exception is that an integer may be given to a function expecting a float.
Strict typing applies to function calls made from within the file with strict typing enabled, not to the functions declared within that file. Enabling strick mode also affects return type declarations.
[PHP 5.6+]Argument lists may incude the ... token to denote that the func accepts a variable number of arguments. The arguments will be passes into the given variable as an array.
function sum(...$numbers){ $acc = 0; foreach($numbers as $n){ $acc += $n; } return $acc; } echo sum(1,2,3,4);
you can also use the ... when calling functions to upack an array or Traversable variable or literal into the argument list.
function add($a,$b){ return $a + $b; } echo add(...[1,2])."\n";
You may specify normal positional arguments before ... token.
You may also pass variable arguments by referance by prefixing the ... with an ampersand(&).
Returning values
Optional return statement. Any type may be returned. The function ends its execution immediately and pass control back to the line from which it was called.
If return is omitted NULL will be returned.
A function cannot return multiple values.
To return a referance from a function, use the ampersand(&) in both the function declaration and when assigning the returned value to a variable.
function &returns_referance(){ ... } $newref =& returns_referance;
Return type declarations supported in PHP 7+.
Variable functions
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.
function foo(){ ... } $func = 'foo'; $func();
Variable functions won’t work with language constructors(echo, include…)
Object methods can also be called with the variable functions syntax.
class Foo { function Variable(){ ... } } $foo = new Foo(); $funcname = "Variable"; $foo -> $ funcname();
When calling static methods, the function call is stronger than the static property operator.
class Foo { static $variable = ""; static function Variable(){ ... } } echo Foo::$variable; $variable = "Variable"; Foo::$variable(); //$foo -> Variable();
You can call any callable stored in a variable.
Internal(built-in) functions
Standard functions and constructors.
Functions that require specific PHP extensions compiled in, otherwise fatal errors will appear.(phpinfo() , get_loaded_extensions() )
If the parameters given to a function are not what it expects, the return value of the function is undefined. In this case it will likely return NULL.
Anonymous function(closures)
Most usefull as the value of callback parameters, but also have other uses.
Closures can be used as the value of variables.
$greet = function($name){ printf("Hello %\r\n", $name); }; $greet('World'); $greet('PHP');
Closures may also inherit variables from the parent scope. Any such variables must be passed to the use language constructor.
$message = 'hello'; $example = function(){ var_dump($message); }; $example(); //wrong!! $example = function() use($message){ var_dump($message); } $example(); //hello $message = 'world'; $example(); //hello $message = 'hello'; $example = function() use(&$message){ var_dump($message); } $example(); //hello $message = 'world'; $example(); //world $example = function($arg) use($message){ var_dump($arg.' '.$message); }; $example("hello"); //hello world
The parent scope of a closure is the function in which the closure was declared(not necessarily the function it was called from).
If the automatic binding to $this of a class is not wanted, then static anonymous funcs may be used instead. This prevents the function from having the current class automatically bound to them.
Is possible to use func_num_args() , func_get_arg() , func_get_args() from within a closure.