php-7

Null Coalesce Operator

Introduction#

The null coalescing operator (??) has been added as syntactic sugar 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.

General usage

// Fetches the value of $_GET['id'] and returns 0 if it does not exist.
$id = $_GET['id'] ?? 0;
// This is equivalent to:
$id = isset($_GET['id']) ? $_GET['id'] : 0;

// Coalescing can be chained: this will return the first defined value out of 
// $_GET['id'], $_POST['id'], and 0.
$id = $_GET['id'] ?? $_POST['id'] ?? 0;

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow