Many programming languages have a coalesce function (returns the first non-NULL value, example). PHP, sadly in 2009, does not.
What would be a good way to implement one in PHP until PHP itself gets a coalesce function?
PHP 7 introduced a real coalesce operator:
echo $_GET['doesNotExist'] ?? 'fallback'; // prints 'fallback'
If the value before the ??
does not exists or is null
the value after the ??
is taken.
The improvement over the mentioned ?:
operator is, that the ??
also handles undefined variables without throwing an E_NOTICE
.