Example 12-2. Conditional functions
<?php
$makefoo = true;
/* We can't call foo() from here
since it doesn't exist yet,
but we can call bar() */
bar();
if ($makefoo) {
function foo ()
{
echo "I don't exist until program execution reaches
me.\n";
}
}
/* Now we can safely call foo()
since $makefoo evaluated to true */
if ($makefoo) foo();
function bar()
{
echo "I exist immediately upon program start.\n";
}
?>
Example 12-3. Functions within functions
<?php
function foo()
{
function bar()
{
echo "I don't exist until foo() is called.\n";
}
}
/* We can't call bar() yet
since it doesn't exist. */
foo();
/* Now we can call bar(),
foo()'s processesing has
made it accessible. */
bar();
?>
PHP does not support function overloading, nor is it possible
to undefine or redefine previously-declared functions.
Note: 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). PHP 4 supports
both: see Variable-length argument lists and the function
references for func_num_args(), func_get_arg(), and func_get_args()
for more information.
|