Example 12-7. Incorrect usage of default
function arguments
<?php
function makeyogurt ($type = "acidophilus", $flavour)
{
return "Making a bowl of $type $flavour.\n";
}
echo makeyogurt ("raspberry"); // won't work
as expected
?>
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:
Example 12-8. Correct usage of default function arguments
<?php
function makeyogurt ($flavour, $type = "acidophilus")
{
return "Making a bowl of $type $flavour.\n";
}
echo makeyogurt ("raspberry"); // works as expected
?>
The output of this example is:
Making a bowl of acidophilus raspberry.
Variable-length argument lists
PHP 4 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.
|