This chapter provides tutorial examples and notes about user-defined functions. Topics include defining function with arguments, calling functions in expressions, passing arguments by values or by references, variable-length argument list, providing default values for arguments, returning data by value or by reference.
As we learned from the previous section, PHP supports pass-by-reference arguments if they are
defined with a & prefix on the argument variables.
If an argument is defined as an pass-by-reference argument, the calling operation can provide a variable,
for this argument. The reference of that variable will be taken and assigned to this argument variable when function is called.
For example, if we have "function f(&$var) {...}" defined, "f($arg)" is equivalent to "{$var=&$arg; ...}".
To see how passing arguments by references works, I wrote this tutorial example, PassByReference.php:
<?php # PassByReference.php
# Copyright (c) 2003 by Dr. Herong Yang. http://www.herongyang.com/
#
function swap(&$left, &$right) {
$temp = $left;
$left = $right;
$right = $temp;
print(" Swapped in function: ".getString($left, $right)."\n");
}
# print("\n 1. Passing two literals:\n");
# print(" Before call: ". getString("Apple", "Orange") ."\n");
# swap("Apple", "Orange");
print("\n 2. Passing two variables:\n");
$x = "Dog"; $y = "Cat";
print(" Before call: ". getString($x, $y) ."\n");
swap($x, $y);
print(" After call: ". getString($x, $y) ."\n");
print("\n 3. Passing two arrays:\n");
$x = array("Mon", "Tue"); $y = array("Jan", "Feb");
print(" Before call: ". getString($x, $y) ."\n");
swap($x, $y);
print(" After call: ". getString($x, $y) ."\n");
print("\n 4. Passing two objects:\n");
$x = new DateTime("2005-01-01"); $y = new DateTime("2006-01-01");
print(" Before call: ". getString($x, $y) ."\n");
swap($x, $y);
print(" After call: ". getString($x, $y) ."\n");
function getString($left, $right) {
if (is_scalar($left)) {
return "$left | $right";
} elseif (is_array($left)) {
return "($left[0], $left[1]...) | ($right[0], $right[1]...)";
} elseif (is_object($left) && get_class($left)=="DateTime") {
return $left->format("Y")." | ".$right->format("Y");
} else {
return NULL;
}
}
?>
If you run this sample script, you should get:
2. Passing two variables:
Before call: Dog | Cat
Swapped in function: Cat | Dog
After call: Cat | Dog
3. Passing two arrays:
Before call: (Mon, Tue...) | (Jan, Feb...)
Swapped in function: (Jan, Feb...) | (Mon, Tue...)
After call: (Jan, Feb...) | (Mon, Tue...)
4. Passing two objects:
Before call: 2005 | 2006
Swapped in function: 2006 | 2005
After call: 2006 | 2005
The output confirms that:
My swap() function is very powerful. It swapped references of any types of variables received from the argument list.
Pass-by-reference arguments can be used for scalar, array and object variables.
Pass-by-reference arguments allow functions to reassign data values to original variables in the calling code.
Pass-by-reference arguments can only take variables from the calling code. If you open test #1 in my sample script,
the execution will fail with a message, "Fatal error: Only variables can be passed by reference".
Exercise: Try to test pass-by-reference arguments with: