Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Variables and Constants

PHP references

Reference for PHP references: the & operator, aliasing variables, reference parameters and returns, foreach by reference pitfalls and references versus objects.

A reference makes two names refer to the same value. You create one with &: after $b = &$a; both $a and $b are aliases for the same underlying value, so changing either changes both. This is different from assignment by value, which copies, and different from pointers in C, because you cannot do arithmetic with a reference or take an address.

References appear in four places: assignment ($b = &$a), function parameters (function f(array &$list)), return values (function &get()), and foreach (foreach ($items as &$item)). They are also used in closures (use (&$total)).

PHP
$a = 1;
$b = &$a;
$b = 2;
echo $a;     // 2

Try it

PHP Aliases, reference parameters and closures

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP foreach by reference and the classic bug

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
$alias = &$variable;
$alias = &$array["key"];
$alias = &$object->property;
function f(array &$param) { }
function &getRef() { static $v = 0; return $v; }
$ref = &getRef();
foreach ($array as $key => &$value) { }
$closure = function () use (&$captured) { };
Operation Effect
unset($b) Removes the name $b; the value lives on through $a.
$b = &$c Rebinds $b to $c's value, detaching it from the old one.
$b = $a where $a is a reference Copies the value; $b is a normal variable.
Reference parameter The caller must pass a variable (or array element, property); a literal or function result is an error.

Usage notes

  • Always unset() the loop variable after foreach (... as &$x). Otherwise the last element stays a reference and a later loop with the same variable name overwrites it (see the second example).
  • Objects already behave like handles. You rarely need & for objects; it only matters when the function must replace which object the caller's variable points to.
  • Arrays are copied on write. Passing a big array by value is cheap. A reference parameter is not faster and can even slow things down, because it blocks some optimizations.
  • References inside arrays survive copies. If an array element is a reference, copying the array keeps sharing that element between copies, which is surprising. Another reason to unset().
  • Built-in functions using references: sort(), array_push(), preg_match() (the $matches parameter), end(), settype(). Their signatures show &$param. The caller must pass a variable.
  • Returning a reference (function &f()) is rare; you must also assign it with =&. Prefer returning objects or values.
  • $GLOBALS and global: global $x binds a local name to the global variable by reference.
  • Readability: prefer returning new values ($list = addItem($list, "y")) to hidden mutation.