Ternary null coalescing
📝 PHPAssigns a default value when a variable is null, using PHP's shorter null coalescing operator.
PHP
$username = $GET['user'] ?? 'guest';
$result = $data['key'] ?? 'default';
$value = $nullableVar ?? 'fallback';
echo $username;
Comments
The null coalescing operator (
??) works well for undefined keys too, not just null values. In your example,$username ?? 'guest'avoids an undefined index notice if$usernameisn't set, which the ternary operator (isset($username) ? $username : 'guest') also handles but with more syntax. Just watch for nested coalescing, as it evaluates left to right and can mask deeper null issues in chained calls like$a ?? $b ?? $c.