PHP 7.0.6 Released

What References Are

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. See What References Are Not for more information. 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.

User Contributed Notes

stilezy
1 month ago
One subtle effect of PHP's assign-by-reference is that operators which might be expected to work with args that are references usually don't.  For example:

$a = ($b ? &$c : &$d);

fails (parser error) but the logically identical

if ($b)
   $a =& $c;
else
   $a =& $cd;

works. It's not always obvious why seemingly identical code throws an error in the first case. This is discussed on a PHP bug report ( https://bugs.php.net/bug.php?id=54740 ). TL;DR version, it acts more like an assignment term ($var1) "=&" ($var2) than a function/operator ($var1) "=" (&$var2).
To Top