Functions
- Function names are case-insensitive, but should be used with the same capitalization they're declared with.
- Arguments can be passed by value (the default), by reference, and via default argument values (for scalar arguments).
- Functions can be called recursively.
- Any valid PHP code may appear inside a function, including other functions and class definitions.
- Function overloading is not supported.
- Functions cannot be undefined or redefined.
Defining functions
function foo( ARGS ) {
statements;
[return VALUE;]
}
- In PHP 3, functions must be defined before they're referenced.
- In PHP 4, functions do not have to be
defined before they are
referenced, unless they're conditionally defined (for instance, defined
within an "
if" block, or within another function).
Calling functions
foo( ARGS );
- Placing an at sign (@) before a function call suppresses the generation of errors.
Value arguments
function foo( $value_arg ) {
statements;
}
foo($var);
- Arguments are passed by value by default.
Reference arguments
function foo( &$ref_arg ) {
statements;
}
foo($var);
- In the function declaration, precede an
argument name with a
"
&" to cause the argument to be automatically passed in by reference.
Default arguments
function foo( $default_arg = VALUE ) {
statements;
}
foo( );
foo($var);
- The default value for a default argument must be a constant expression, not (for example) a variable or class member.
- Any default arguments should be defined on the right side of any non-default arguments.
- PHP 3 and PHP 4 supports default arguments.
Variable numbers of arguments
- No special syntax is required to define a function with variable numbers of arguments
- Any number of extra arguments can be passed to any function.
- Related functions:
func_num_args(),func_get_arg(),func_get_args()
- PHP 4 supports variable numbers of arguments.
Returning a value
- A function can return a value by using the optional return statement.
- Any type can be returned, including lists and objects.
- The return statement causes the function to end its execution immediately.
- Functions that do not explicitly return a value will implicitly return the value NULL.
- If a function returns an array, the
elements can be extracted
using
list(), orextract().
return $var;
return $var + $var;
return array (0, 1, 2);
function fn($a, $b) {
return array(
$a * $b,
$a + $b,
);
}
list($product, $sum) = fn(3, 4);
Returning a reference
- To return a reference from a function,
use the reference
operator "
&" in both the function declaration and when assigning the returned value to a variable.
function &returns_reference() {
return $someref;
}
$newref =& returns_reference();
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.
- This can be used to implement callbacks, function tables, etc.
- Variable functions won't work with
language constructs such
as
echo(),print(),unset(),isset(),empty(),include(),require()and the like; wrapper functions can be created to utilize these constructs indirectly as variable functions. - An object method can also be called using a variable function.
- Related functions:
call_user_func(),function_exists()
function foo() {
statements;
}
$func = 'foo';
$func(); // This calls foo()
class my_class {
function some_method() {
statements;
}
}
$object = new my_class();
$method_name = "some_method";
$object->$method_name(); // This calls $object->some_method()
Resources URL:
notes/php/resources
Sources URL:
notes/php/sources