Null Coalescing Operator Chains in PHP
Coalescing operators can be called one after another in a chain. In the following example, the variable will be assigned either the first element of the array, or the second element of the array, or a given string, if these elements do not exist:
<?php
$elem = $arr['test1'] ?? $arr['test2'] ?? 'empty';
?>
Rewrite the following code using the studied operator:
<?php
if (isset($user['name'])) {
$res = $user['name'];
} elseif (isset($user['surname'])) {
$res = $user['surname'];
} else {
$res = '';
}
?>