User-defined functionsA function may be defined using syntax such as the following:
Example #1 Pseudo code to demonstrate function uses
<?php Any valid PHP code may appear inside a function, even other functions and class definitions. Function names follow the same rules as other labels in PHP. A valid function 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]*. Tip
See also the Userland Naming Guide. 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 manner such as the two examples shown. Its definition must be processed prior to being called.
Example #2 Conditional functions
<?php
Example #3 Functions within functions
<?php All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa. PHP does not support function overloading, nor is it possible to undefine or redefine previously-declared functions.
Both variable number of arguments and default arguments are supported in functions. See also the function references for func_num_args, func_get_arg, and func_get_args for more information. It is possible to call recursive functions in PHP. However avoid recursive function/method calls with over 100-200 recursion levels as it can smash the stack and cause a termination of the current script. Example #4 Recursive functions
<?php |