on-internet
← Community

Using PHP's null coalescing operator on an array

Stack Overflow ·

I am using PHP's null coalescing operator; Null coalescing operator 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. <?php // Fetches the value of $_GET['user'] and returns 'nobody' // if it does not exist. $username = $_GET['user'] ?? 'nobody'; // This is equivalent to: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody'; // Coalescing can be chained: this will return the first // defined val

Comments

Sign in or sign up to comment and upvote.

No comments yet.