Null Coalescing Operator

Null Coalescing Operator

The null coalescing operator (??) has been added for the common case of needing to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not NULL; otherwise it returns its second operand. This operator can be used instead of using isset() along with the ternary operator (?:)

<?php
// Fetches the value of $_GET['user'] and returns 'not sent'
// if it does not exist.
echo $_GET['user'] ?? 'not sent';
// This is equivalent to:
echo isset($_GET['user']) ? $_GET['user'] : 'not sent';
// Coalescing can be chained: this will return the first
// defined value out of $_GET['user'], $_POST['user'], and
// 'not sent'.
echo $_GET['user'] ?? $_POST['user'] ?? 'not sent';
?>

Output:

not sent
not sent
not sent