References

What References Are

Documentation link
References in PHP are a means to access the same variable content by different names. They are not like C pointers; for instance, you cannot perform pointer arithmetic using them, they are not actual memory addresses, and so on. Instead, they are symbol table aliases. Note that in PHP, variable name and variable content are different, so the same content can have different names. The closest analogy is with Unix filenames and files - variable names are directory entries, while variable content is the file itself. References can be likened to hardlinking in Unix filesystem.

What References Do

Documentation link
There are three basic operations performed using references: assigning by reference, passing by reference, and returning by reference.

Assign By Reference

In the first of these, PHP references allow you to make two variables refer to the same content. Meaning, when you do:

1
2
3
<?php
$a =& $b;
?>
it means that $a and $b point to the same content.

Note

$a and $b are completely equal here. $a is not pointing to $b or vice versa. $a and $b are pointing to the same place.

Pass By Reference

Documentation link
The second thing references do is to pass variables by reference. This is done by making a local variable in a function and a variable in the calling scope referencing the same content. Example:

1
2
3
4
5
6
7
8
9
<?php
function foo(&$var)
{
    $var++;
}

$a=5;
foo($a);
?>
will make $a to be 6. This happens because in the function foo the variable $var refers to the same content as $a.

Return By Reference

Documentation link
Returning by reference is useful when you want to use a function to find to which variable a reference should be bound. Do not use return-by-reference to increase performance. The engine will automatically optimize this on its own. Only return references when you have a valid technical reason to do so. To return references, use this syntax:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<?php
class foo {
    public $value = 42;

    public function &getValue() {
        return $this->value;
    }
}

$obj = new foo;
$myValue = &$obj->getValue(); // $myValue is a reference to $obj->value, which is 42.
$obj->value = 2;
echo $myValue;                // prints the new value of $obj->value, i.e. 2.
?>

What References Are Not

What References Are Not
As said before, references are not pointers. That means, the following construct won't do what you expect:

1
2
3
4
5
6
7
<?php
function foo(&$var)
{
    $var =& $GLOBALS["baz"];
}
foo($bar); 
?>
What happens is that $var in foo will be bound with $bar in the caller, but then 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.

Unsetting References

Documentation link
When you unset the reference, you just break the binding between variable name and variable content. This does not mean that variable content will be destroyed. For example:

1
2
3
4
5
<?php
$a = 1;
$b =& $a;
unset($a); 
?>
won't unset $b, just $a. Again, it might be useful to think about this as analogous to the Unix unlink call.

Spotting References

Documentation link
Many syntax constructs in PHP are implemented via referencing mechanisms, so everything mentioned herein about reference binding also applies to these constructs. Some constructs, like passing and returning by reference, are mentioned above. Other constructs that use references are:

global References

When you declare a variable as global $var you are in fact creating reference to a global variable. That means, this is the same as:

1
2
3
4
<?php
$var =& $GLOBALS["var"];
?>
This also means that unsetting `$var` won't unset the global variable.

$this

In an object method, $this is always a reference to the called object.