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 operator (??) is a binary operator that evaluates and returns its left operand if it is set and not null. If the left operand is undefined or evaluates to null, it evaluates and returns the right operand. It serves as an optimized syntactic alternative to a ternary operation combined with an isset() check.
$result = $expression1 ?? $expression2;

Evaluation Mechanics

Implicit isset() Check and Single Evaluation The ?? operator safely handles undefined variables and uninitialized array keys. It suppresses E_NOTICE or E_WARNING diagnostics that would normally occur when accessing an undefined memory location. Crucially, the ?? operator evaluates its left operand exactly once. If the left operand is an expression with side effects, the traditional ternary isset() approach evaluates the expression twice, whereas ?? prevents this redundant evaluation.
// Evaluates $array[expensiveFunction()] exactly once
$result = $array[expensiveFunction()] ?? $b;

// Evaluates $array[expensiveFunction()] twice (once for isset(), once for the return)
$result = isset($array[expensiveFunction()]) ? $array[expensiveFunction()] : $b;
Strict Null vs. Falsy Evaluation Unlike the shorthand elvis operator (?:), which evaluates the left operand for truthiness, the ?? operator strictly checks for null. Standard falsy values (such as 0, 0.0, "", false, or []) are considered valid, non-null values and will be returned if they are the left operand.
$falsyVar = 0;

// Null Coalescing (??) checks for null
$result1 = $falsyVar ?? 100; // Returns: 0

// Elvis Operator (?:) checks for truthiness
$result2 = $falsyVar ?: 100; // Returns: 100
Short-Circuiting The operator employs short-circuit evaluation. If the left operand is resolved as set and not null, the PHP engine bypasses the evaluation of the right operand entirely. This is critical when the right operand contains a function call or a computationally expensive expression.
$a = 'value';
// expensiveFunction() is never executed
$result = $a ?? expensiveFunction(); 

Associativity and Chaining

The ?? operator is right-associative. When multiple null coalescing operators are chained, the engine evaluates the expression from left to right, returning the first operand that exists and is not null.
$result = $x ?? $y ?? $z ?? 'fallback';

Null Coalescing Assignment (??=)

Introduced in PHP 7.4, the null coalescing assignment operator is a compound operator that assigns the right operand to the left operand only if the left operand is currently null or undefined. Its primary technical advantage is preventing the double-evaluation of the left-hand side expression and avoiding redundant write operations. The ??= operator evaluates its left operand exactly once and completely skips the assignment operation if the variable is already set and not null. Conversely, using $a = $a ?? $b; evaluates the left side twice (if it is an expression) and performs a redundant write operation even if the value is not null, which can trigger unintended side effects such as __set() magic methods.
// Evaluates $obj->prop once. Skips the write operation entirely if already set and not null.
$obj->prop ??= $b;

// Evaluates $obj->prop twice. Performs a redundant write, potentially triggering __set().
$obj->prop = $obj->prop ?? $b;
Master PHP with Deep Grasping Methodology!Learn More