search for in the  
<What References DoPassing by Reference>
Last updated: Thu, 19 May 2005

What References Are Not

As said before, references aren't pointers. That means, the following construct won't do what you expect:

<?php
function foo(&$var)
{
  
$var =& $GLOBALS["baz"];
}
foo($bar);
?>

What happens is that $var in foo will be bound with $bar in caller, but then it will be re-bound with $GLOBALS["baz"]. There's no way to bind $bar in the calling scope to something else using the reference mechanism, since $bar is not available in the function foo (it is represented by $var, but $var has only variable contents and not name-to-value binding in the calling symbol table). You can use returning references to reference variables selected by the function.



User Contributed Notes
What References Are Not
nicolas at serpe dot org
24-May-2004 12:47
About ansonyumo at email dot com second example, output will be 99, not 9.
ansonyumo at email dot com
28-Feb-2004 03:47
The assertion,  "references are not like pointers," is a bit confusing.

In the example, the author shows how assigning a reference to a formal parameter that is also a reference does not affect the value of the actual parameter. This is exactly how pointers behave in C. The only difference is that, in PHP, you don't have to dereference the pointer to get at the value.

-+-+-
int bar = 99;

void foo(int* a)
{
   a = &bar;
}

int main()
{
   int baz = 1;
   foo(&baz);
   printf("%d\n", baz);
   return 0;
}
-+-+-

The output will be 1, because foo does not assign a value to the dereferenced formal parameter. Instead, it reassigns the formal parameter within foo's scope.

Alternatively,
-+-+-
int bar = 99;

void foo(int* a)
{
   *a = bar;
}

int main()
{
   int baz = 1;
   foo(&baz);
   printf("%d\n", baz);
   return 0;
}
-+-+-

The output will be 9, because foo dereferenced the formal parameter before assignment.

So, while there are differences in syntax, PHP references really are very much like pointers in C.

I would agree that PHP references are very different from Java references, as Java does not have any mechanism to assign a value to a reference in such a way that it modifies the actual parameter's value.
schultz __at__ widescreen __dot__ ch
26-Jan-2004 09:57
A not so simple Workaround...but still doable...have fun

class My{
   var $value;
  
   function get1(&$ref){
       $ref[] =& $this;
   }
  
   function get2(&$ref){
       $ref =& $this;
   }
  
   function get3(&$ref){
       $ref = $this;
   }
}

$m = new My();

$m->value = 'foo';
$m->get1($ref=array());
$m1 =& $ref[0];
$m1->value = 'bar';
echo "\n".'Works but is ugly...';
echo "\n".' m:'. get_class($m) . '->value = '. $m->value;
echo "\n".' m1:'. get_class($m1) . '->value = '. $m1->value;

echo "\n".'Does not work because references are not pointers...';
$m->value = 'foo';
$m->get2($m2);
$m2->value = 'bar';
echo "\n".' m:'. get_class($m) . '->value = '. $m->value;
echo "\n".' m2:'. get_class($m2) . '->value = '. $m2->value;

$m->value = 'foo';
$m->get3($m3);
$m3->value = 'bar';
echo "\n".'Does not work becuase it is set to a copy';
echo "\n".' m:'. get_class($m) . '->value = '.$m->value;
echo "\n".' m3:'. get_class($m3) . '->value = '. $m3->value;
christian at kno dot at
12-Oct-2001 08:04
As said above references are not pointers.

Following example shows a difference between pointers and references.

This Code
<?
   $b
= 1;
  
$a =& $b;

   print(
"<pre>");
   print(
"\$a === \$b: ".(($a === $b) ? "ok" : "failed")."\n");
   print(
"unsetting \$a...\n");
   unset(
$a);
   print(
"now \$a is ".(isset($a) ? "set" : "unset")." and \$b is ".(isset($b) ? "set" : "unset")."\n");
   print(
"</pre>");

  
$b = 1;
  
$a =& $b;

   print(
"<pre>");
   print(
"\$a === \$b: ".(($a === $b) ? "ok" : "failed")."\n");
   print(
"unsetting \$b...\n");
   unset(
$b);
   print(
"now \$a is ".(isset($a) ? "set" : "unset")." and \$b is ".(isset($b) ? "set" : "unset")."\n");
   print(
"</pre>");
?>

will produce this output:
---------
$a === $b: ok
unsetting $a...
now $a is unset and $b is set

$a === $b: ok
unsetting $b...
now $a is set and $b is unset
---------

So you see that $a and $b are identical ($a === $b -> true), but if one of both is unset, the other is not effected.

<What References DoPassing by Reference>
 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