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 functions in two distinct capacities depending on its syntactic context: as a Bitwise AND operator for binary-level evaluation, and as a Reference operator for memory aliasing.

1. Bitwise AND Operator

When placed between two operands, & performs a bitwise AND operation. The behavior and return type depend on the types of the operands:
  • Integer Evaluation: If the operands are integers (or mixed integer/string), it evaluates their binary representations and returns a new integer. Each bit in the result is set to 1 only if the corresponding bits in both operands are 1. If either bit is 0, the resulting bit is 0.
  • String Evaluation: If both operands are strings, the & operator performs a bitwise AND operation on the ASCII values of the characters at corresponding byte positions and returns a new string.
$intResult = $a & $b;
$strResult = $stringA & $stringB;
Integer Mechanics:
$a = 5;  // Binary: 0101
$b = 3;  // Binary: 0011

$result = $a & $b; 

// Binary evaluation:
//   0101
// & 0011
// ---
//   0001  (Decimal: 1)
String Mechanics:
$str1 = "A"; // ASCII 65: 01000001
$str2 = "B"; // ASCII 66: 01000010

$result = $str1 & $str2; 

// Binary evaluation:
//   01000001
// & 01000010
// -------
//   01000000  (ASCII 64: "@")

2. Reference Operator

When prepended to a variable, function parameter, closure capture, or function declaration, & acts as the reference operator. In modern PHP (PHP 7 and later), it instructs the engine to create separate zval structures of type IS_REFERENCE that point to a shared zend_reference struct, bypassing PHP’s default pass-by-value and copy-on-write behaviors. Assignment by Reference: Binds two variables to the same zend_reference. Modifying either variable alters the shared value.
$a = 10;
$b = &$a; 
$b = 20; 
// $a evaluates to 20
Pass by Reference: Applied in function signatures to bind the caller’s variable to the function’s local parameter scope. Mutations to the parameter directly affect the original variable.
function mutateValue(&$parameter) {
    $parameter++;
}

$value = 5;
mutateValue($value);
// $value evaluates to 6
Return by Reference: Applied to a function declaration to return a reference to a variable rather than a copy of its value. The & must be present in both the function signature and the receiving assignment.
function &getReference() {
    static $internalState = 0;
    return $internalState;
}

$stateAlias = &getReference();
$stateAlias = 42;
// The static $internalState is now 42
Closure Capture by Reference: Applied within the use construct of an anonymous function (closure) to capture a variable from the parent scope by reference, allowing the closure to mutate the original variable.
$counter = 0;
$increment = function() use (&$counter) {
    $counter++;
};

$increment();
// $counter evaluates to 1
Iteration by Reference: Applied to the value variable in a foreach construct to mutate array elements directly in memory during iteration.
$array = [1, 2, 3];
foreach ($array as &$item) {
    $item *= 2;
}
// $array evaluates to [2, 4, 6]
unset($item); // Destroys the reference binding to the last element
Master PHP with Deep Grasping Methodology!Learn More