The Null Coalescing Operator
$x ?? $default evaluates to $x if $x exists and isn't null, or to $default otherwise — and critically, unlike reading a missing array key directly, it does this with no warning about an undefined key or variable. It's the concise, idiomatic replacement for writing isset($x) ? $x : $default by hand, and it's everywhere in real PHP code, especially for reading optional array entries (the same shape $_GET and $_POST entries take in a real HTTP request).
??= (null coalescing assignment, added in PHP 7.4 — confirmed working on this site's runtime) assigns to a variable only if it's currently unset or null, and does nothing otherwise — useful for filling in a default exactly once without an explicit if around it.
<?php $config = ["theme" => "dark"]; $theme = $config["theme"] ?? "light"; $lang = $config["lang"] ?? "en"; echo "$theme $lang\n"; $settings = []; $settings["retries"] ??= 3; $settings["retries"] ??= 99; echo $settings["retries"], "\n";
dark en 3
$config["theme"] exists, so $theme ?? "light" short-circuited to the real value "dark"; $config["lang"] doesn't exist at all, so $lang fell back to "en" with no undefined-key warning either way. The second ??= on $settings["retries"] did nothing, because the first one had already set it to 3 — it stayed 3, not 99. (PHP 8.0 also added a match expression as a more concise alternative to switch for this kind of value-mapping — worth knowing the name if you read newer PHP code, but this site's confirmed runtime is PHP 7.4.1, so this course sticks to ?? and the switch statement from the control-flow lesson rather than showing match code that wouldn't actually run here.) Next: closures and arrow functions — anonymous functions, the use keyword, and PHP 7.4's concise fn() syntax.
Practice exercise
Given $settings = ["theme" => "dark"], use ?? to read $settings["fontSize"] with a default of 14 into $fontSize, then echo $fontSize.