Skip to content
Justice Tech School

Null Coalescing

?? returns the left value if it exists and is not null, otherwise the right. ??= assigns only if missing.

Example
$a = null;
echo $a ?? "default", "\n";
$arr = [];
$arr["k"] ??= 5;
echo $arr["k"], "\n";
echo $undefined ?? "none";

Result

default
5
none

Exercise

Default value.

$x  "none"

Quiz Test

What does ?? check?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.