Returning References

Returning by-refernce it is useful when you want to use function to find variable which should be bound to. When returning references, use this syntax:


function &find_var ($param) {
    ...code...
    return $found_var;
}

$foo =& find_var ($bar);
$foo->x = 2; 
     

In this example, property of the object returned by the find_var function would be set, not of the copy, as it would be without using reference syntax.

Note: Unlike parameter passing, here you have to use & in both places - to indicate that you return by-reference, not a copy as usual, and to indicate than reference binding and not usual assignment should be done for $foo.