FunctionsUser-defined functions
A function may be defined using syntax such as the following:
Pseudo code to demonstrate function uses
]]>
Any valid PHP code may appear inside a function, even other
functions and class
definitions.
In PHP 3, functions must be defined before they are referenced. No
such requirement exists since PHP 4. Except when
a function is conditionally defined such as shown in the two examples
below.
When a function is defined in a conditional manner such as the two
examples shown. Its definition must be processed prior
to being called.
Conditional functions
]]>
Functions within functions
]]>
PHP does not support function overloading, nor is it possible to
undefine or redefine previously-declared functions.
Function names are case-insensitive, though it is usually good form
to call functions as they appear in their declaration.
PHP 3 does not support variable numbers of arguments to functions,
although default arguments are supported (see Default argument
values for more information). Both are supported, as of PHP 4: see Variable-length argument
lists and the function references for
func_num_args,
func_get_arg, and
func_get_args for more information.
Function arguments
Information may be passed to functions via the argument list,
which is a comma-delimited list of expressions.
PHP supports passing arguments by value (the default), passing by
reference, and default argument
values. Variable-length argument lists are supported only
in PHP 4 and later; see Variable-length argument
lists and the function references for
func_num_args,
func_get_arg, and
func_get_args for more information. A
similar effect can be achieved in PHP 3 by passing an array of
arguments to a function:
Passing arrays to functions
]]>
Making arguments be passed by reference
By default, function arguments are passed by value (so that if
you change the value of the argument within the function, it does
not get changed outside of the function). If you wish to allow a
function to modify its arguments, you must pass them by
reference.
If you want an argument to a function to always be passed by
reference, you can prepend an ampersand (&) to the argument
name in the function definition:
Passing function parameters by reference
]]>
Default argument values
A function may define C++-style default values for scalar
arguments as follows:
Use of default parameters in functions
]]>
The output from the above snippet is:
Making a cup of cappuccino.
Making a cup of espresso.
Also PHP allows you to use arrays and special type NULL as
default values, for example:
Using non-scalar types as default values
]]>
The default value must be a constant expression, not (for
example) a variable, a class member or a function call.
Note that when using default arguments, any defaults should be on
the right side of any non-default arguments; otherwise, things
will not work as expected. Consider the following code snippet:
Incorrect usage of default function arguments
]]>
The output of the above example is:
Warning: Missing argument 2 in call to makeyogurt() in
/usr/local/etc/httpd/htdocs/php3test/functest.html on line 41
Making a bowl of raspberry .
Now, compare the above with this:
Correct usage of default function arguments
]]>
The output of this example is:
Making a bowl of acidophilus raspberry.
As of PHP 5, default values may be passed by reference.
Variable-length argument lists
PHP 4 and above has support for variable-length argument lists in
user-defined functions. This is really quite easy, using the
func_num_args,
func_get_arg, and
func_get_args functions.
No special syntax is required, and argument lists may still be
explicitly provided with function definitions and will behave as
normal.
Returning values
Values are returned by using the optional return statement. Any
type may be returned, including lists and objects. This causes the
function to end its execution immediately and pass control back to
the line from which it was called. See return
for more information.
Use of return
]]>
You can't return multiple values from a function, but similar
results can be obtained by returning a list.
Returning an array to get multiple values
]]>
To return a reference from a function, you have to use
the reference operator & in both the function declaration and
when assigning the returned value to a variable:
Returning a reference from a function
]]>
For more information on references, please check out References Explained.
Variable functions
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, include,
require and the like. You need to use
your own wrapper function to utilize any of these constructs
as variable functions.
Variable function example
\n";
}
function bar($arg = '')
{
echo "In bar(); argument was '$arg'. \n";
}
// This is a wrapper function around echo
function echoit($string)
{
echo $string;
}
$func = 'foo';
$func(); // This calls foo()
$func = 'bar';
$func('test'); // This calls bar()
$func = 'echoit';
$func('test'); // This calls echoit()
?>
]]>
You can also call an object's method by using the variable functions
feature.
Variable method example
$name(); // This calls the Bar() method
}
function Bar()
{
echo "This is Bar";
}
}
$foo = new Foo();
$funcname = "Variable";
$foo->$funcname(); // This calls $foo->Variable()
?>
]]>
See also call_user_func,
variable variables and function_exists.
Internal (built-in) functions
PHP comes standard with many functions and constructs. There are also
functions that require specific PHP extensions compiled in otherwise
you'll get fatal "undefined function" errors. For example, to use
image functions such as
imagecreatetruecolor, you'll need your PHP compiled
with GD support. Or, to use mysql_connect you'll
need your PHP compiled in with MySQL
support. There are many core functions that are included in every
version of PHP like the string and
variable functions. A call
to phpinfo or
get_loaded_extensions will show you which
extensions are loaded into your PHP. Also note that many extensions are
enabled by default and that the PHP manual is split up by extension.
See the configuration,
installation, and individual
extension chapters, for information on how to setup your PHP.
Reading and understanding a function's prototype is explained within the
manual section titled
how to read a function definition.
It's important to realize what a function returns or if a function works
directly on a passed in value. For example,
str_replace will return the modified string while
usort works on the actual passed in variable
itself. Each manual page also has specific information for each
function like information on function parameters, behavior changes,
return values for both success and failure, and availability information.
Knowing these important (yet often subtle) differences is crucial for
writing correct PHP code.
See also function_exists,
the function reference,
get_extension_funcs, and
dl.