Chapter 4. Basic Language Changes
PHP 7.0 introduces numerous small changes to the language, new operators, functions, and changes to existing functions and constructs.
Operators
PHP 7.0 adds two new operators, the null coalesce operator and the combined comparison operator.
Null Coalesce Operator
The new null coalesce operator ?? will return the left operand if it is not null; otherwise, it returns the right operand. The most interesting part of this operator is that it will not emit a notice if the operand is a nonexistent variable—similar to isset().
Effectively, this is a shortcut for isset() combined with a ternary for assignment. Example 4-1 shows this new, more compact syntax compared to the older syntax.
Example 4-1. Null coalesce operator
$foo=isset($bar)?$bar:$baz;$foo=$bar??$baz;
The two lines in the preceding example are functionally identical.
You can also nest the operator, and it will return the first non-null (or the last argument), as shown in Example 4-2.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access