PHP Return Reference
Description
As well as passing variables by reference into functions, you can also get functions to return references, rather than values.
Syntax
Place an ampersand before the function name in your function definition. Then, when you return a variable with the return statement, you pass a reference to that variable back to the calling code, rather than the variable ' s value:
function &myFunc(){
// (do stuff)
return $var; // Returns a reference to $var
}
Example
Return reference from a function
<?PHP/*from w ww. ja va2 s . c om*/
$myNumber = 5;
function &getMyNumber() {
global $myNumber;
return $myNumber;
}
$numberRef = &getMyNumber();
$numberRef++;
echo "\$myNumber = $myNumber\n"; // Displays "6"
echo "\$numberRef = $numberRef\n"; // Displays "6"
?>
The code above generates the following result.