PHP Tutorial PHP Advanced PHP Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

PHP - Null coalescing operator

The PHP null coalescing operator ?? returns right-hand side operand when left-hand side operand is NULL or do not exist, otherwise returns left-hand side operand.


The example below shows how to use null coalescing operator.

$nullValue = null;
$Num = 50;

$valA = $nullValue ?? "default for A";
$valB = $Num ?? 0;

echo $valA ."\n";
echo $valB ."\n";

The output of the above code will be:

default for A

❮ PHP - Operators