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 ??= (null coalescing assignment) operator evaluates its left operand and assigns the value of its right operand to it strictly if the left operand is null or undefined. If the left operand holds any non-null value, the assignment is bypassed entirely, and the right operand is not evaluated. Introduced in PHP 7.4, it provides a syntactic shorthand for conditional initialization based on nullability.

Syntax

$leftOperand ??= $rightOperand;

Logical Equivalence

The internal behavior of the null coalescing assignment operator is functionally equivalent to a conditional isset() check.
// Using the null coalescing assignment operator
$a ??= $b;

// Is functionally equivalent to the following control structure:
if (!isset($a)) {
    $a = $b;
}

Technical Characteristics

  • Single Evaluation: The operator evaluates its left operand exactly once. If the left operand is a complex expression (such as a function returning a reference, or a property access), it is not evaluated twice (once to read, once to write).
  • Assignment Bypass: If the left operand is already set and not null, the assignment operation is completely bypassed. This is a critical distinction from the expression $a = $a ?? $b. The latter always performs an assignment, which can trigger unintended side effects such as invoking __set() on objects or offsetSet() on ArrayAccess implementations. The ??= operator avoids these side effects by skipping the assignment step entirely when the value is not null.
  • Short-Circuit Evaluation: The operator utilizes short-circuiting. If $leftOperand is determined to be set and not null, the $rightOperand is completely ignored, preventing unnecessary execution of functions or expressions placed on the right side.
  • Undefined Variable Handling: Like isset() and ??, the ??= operator safely checks the left operand. If the variable does not exist in the current scope, PHP will not emit an E_WARNING (or E_NOTICE in PHP < 8.0). It will simply initialize the variable with the right operand.
  • Return Value: The expression evaluates to the final value of the left operand after the operation completes, allowing for chained assignments (e.g., $a = $b ??= $c;).

Evaluation Matrix

The operator strictly checks for null. It does not perform truthiness checks. Falsy values such as 0, false, "" (empty string), or [] (empty array) will prevent the assignment.
$var = null;
$var ??= 'assigned'; 
// $var is now 'assigned'

unset($undefinedVar);
$undefinedVar ??= 'assigned'; 
// $undefinedVar is initialized to 'assigned'

$var = false;
$var ??= 'assigned'; 
// $var remains false (false is not null)

$var = 0;
$var ??= 100; 
// $var remains 0 (0 is not null)

$var = "";
$var ??= "default"; 
// $var remains "" (empty string is not null)
Master PHP with Deep Grasping Methodology!Learn More