PHP Tutorial PHP Advanced PHP References

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.

Example:

The example below shows how to use null coalescing operator.

<?php 
$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
50

❮ PHP - Operators