Skip to main content
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.
Integer Mechanics:
String Mechanics:

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.
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.
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.
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.
Iteration by Reference: Applied to the value variable in a foreach construct to mutate array elements directly in memory during iteration.
Tired of Poor PHP Skills? Fix That With Deep Grasping!Learn More