Example 12-5. Passing function parameters
by reference
<?php
function add_some_extra(&$string)
{
$string .= 'and something extra.';
}
$str = 'This is a string, ';
add_some_extra($str);
echo $str; // outputs 'This is a string, and something extra.'
?>
Default argument values
A function may define C++-style default values for scalar
arguments as follows:
Example 12-6. Use of default parameters in functions
<?php
function makecoffee ($type = "cappuccino")
{
return "Making a cup of $type.\n";
}
echo makecoffee ();
echo makecoffee ("espresso");
?>
The output from the above snippet is:
Making a cup of cappuccino.
Making a cup of espresso.
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:
|