Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Coalesce Operator

The coalesce operator ??, also called the null coalesce or null ternary operator, returns its first operand if it is set and not NULL. Otherwise it will return its second operand. It was introduced in version 7.0 to replace the common but verbose isset($x) ? $x : $default pattern, while also silencing the notice that would normally be raised when reading an undefined variable, array key, or property.

The operator is chainable: $a ?? $b ?? $c evaluates left to right and returns the first operand that is set and not NULL, falling through to the last operand otherwise. Since PHP 7.4, it also has an assignment form, ??=, which assigns the right-hand value only if the left-hand variable is currently unset or NULL, making it convenient for lazily initializing defaults.

Because ?? only checks for NULL, it is not a general-purpose replacement for the ternary operator: falsy-but-non-NULL values such as 0, "", or false are returned unchanged rather than replaced by the fallback.

<?php

    class X { }
    
    // instantiation
    $x = new X;
    
    // cloning
    $y = clone $x;

?>

Documentation

See Also