search for in the  
<Passing by ReferenceUnsetting References>
Last updated: Thu, 19 May 2005

Returning References

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

<?php
function &find_var($param)
{
  
/* ...code... */
  
return $found_var;
}

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

In this example, the property of the object returned by the find_var function would be set, not 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 that reference binding, rather than usual assignment, should be done for $foo. & by function definition is optional in class methods.



User Contributed Notes
Returning References
obscvresovl at NOSPAM dot hotmail dot com
24-Dec-2004 03:09
An example of returning references:

<?

$var
= 1;
$num = NULL;

function &
blah()
{
  
$var =& $GLOBALS["var"]; # the same as global $var;
  
$var++;
   return
$var;
}

$num = &blah();

echo
$num; # 2

blah();

echo
$num; # 3

?>

Note: if you take the & off from the function, the second echo will be 2, because without & the var $num contains its returning value and not its returning reference.
hawcue at yahoo dot com
16-Mar-2004 09:58
Be careful when using tinary operation condition?value1:value2

See the following code:

$a=1;
function &foo()
{
  global $a;
  return isset($a)?$a:null;
}
$b=&foo();
echo $b;  // shows 1
$b=2;
echo $a;  // shows 1 (not 2! because $b got a copy of $a)

To let $b be a reference to $a, use "if..then.." in the function.
contact at infopol dot fr
12-Feb-2004 11:36
A note about returning references embedded in non-reference arrays :

<?
$foo
;

function
bar () {
   global
$foo;
  
$return = array();
  
$return[] =& $foo;
   return
$return;
}

$foo = 1;
$foobar = bar();
$foobar[0] = 2;
echo
$foo;
?>

results in "2" because the reference is copied (pretty neat).
zayfod at yahoo dot com
03-Dec-2003 11:23
There is a small exception to the note on this page of the documentation. You do not have to use & to indicate that reference binding should be done when you assign to a value passed by reference the result of a function which returns by reference.

Consider the following two exaples:

<?php

function    & func_b ()
{
  
$some_var = 2;
   return
$some_var;
}

function   
func_a (& $param)
{
  
# $param is 1 here
  
$param = & func_b();
  
# $param is 2 here
}

$var = 1;
func_a($var);
# $var is still 1 here!!!

?>

The second example works as intended:

<?php

function    & func_b ()
{
  
$some_var = 2;
   return
$some_var;
}

function   
func_a (& $param)
{
  
# $param is 1 here
  
$param = func_b();
  
# $param is 2 here
}

$var = 1;
func_a($var);
# $var is 2 here as intended

?>

(Experienced with PHP 4.3.0)
i at tc dot se
08-Oct-2003 08:54
In the following example, if $key does not exist in the "private" $arr when calling get_value_for($key), then $this->arr[$key] will be set, which is not the intended action.

class silly_array {
   var $arr;
   ...
   function & get_value_for($key) {
       return $this->arr[$key];
   }
   ...
}

Therefore, one should check if $key exists in the array first, before returning its value. Something like this:

function & get_value_for($key) {
   return isset($this->arr[$key]) ? $this->arr[$key] : false;
}

<Passing by ReferenceUnsetting References>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT