Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The =& operator in PHP is the assignment by reference operator. It binds two variable names to the same underlying data. In modern PHP (PHP 7 and later), variables are represented by zval structures. When =& is used, the zvals for both variables are assigned the type IS_REFERENCE and are updated to point to a shared zend_reference structure that holds the actual value. This makes both variables aliases of each other within the symbol table.
$target =& $source;
Unlike standard assignment (=), which utilizes a copy-on-write mechanism, =& forces both variables to point to the exact same zend_reference. Consequently, any modification, reassignment, or type change applied to one variable is immediately reflected in the other.
$a = 100;
$b =& $a;

$b = 200;
// $a now evaluates to 200

Implicit Initialization

A unique behavior of the =& operator is implicit initialization. If an undefined variable is assigned by reference, PHP silently initializes the undefined variable to null and establishes the reference binding. Unlike standard assignment, this does not trigger an “Undefined variable” warning.
// $uninitialized is not defined prior to this line
$a =& $uninitialized; 

// $uninitialized is silently created and set to null.
// $a is now a reference to $uninitialized (value: null).

Internal Behavior

It is critical to understand that =& does not create C-style memory pointers. Instead, it creates symbol table aliases. You cannot perform pointer arithmetic, and you are not pointing to a raw memory address directly. If a new value is assigned by reference to a variable that is already a reference, the previous binding is broken for that specific variable, but the original data remains intact for any other aliases.
$a = 1;
$b =& $a;
$c = 2;

$b =& $c; 
// $b now aliases $c. 
// $a remains 1 and is no longer linked to $b.

Unsetting References

Using unset() on a variable assigned by reference does not destroy the underlying data or affect the other aliases. It merely removes the specific variable name from the symbol table, breaking the reference binding.
$x = 50;
$y =& $x;

unset($y);
// $y is removed from the symbol table.
// $x remains 50. The shared zend_reference is untouched.

Function Returns

The =& operator is strictly required when binding a variable to a function that returns by reference. To establish the reference binding, both the function declaration (using &) and the assignment (using =&) must explicitly declare the reference.
function &returnByRef(&$param) {
    return $param;
}

$original = "data";
// The =& operator binds $refVar to the shared zend_reference of $original
$refVar =& returnByRef($original); 
If standard assignment (=) is used instead of =& on a function returning by reference, PHP will perform a standard value copy, ignoring the reference return type.
Master PHP with Deep Grasping Methodology!Learn More