If you use the shortcut ternary operator like this, it will cause a notice if $_GET['username']
is not set:
$val = $_GET['username'] ?: 'default';
So instead you have to do something like this:
$val = isset($_GET['username']) ? $_GET['username'] : 'default';
The null coalescing operator is equivalent to the above statement, and will return 'default' if $_GET['username']
is not set or is null
:
$val = $_GET['username'] ?? 'default';
Note that it does not check truthiness. It checks only if it is set and not null.
You can also do this, and the first defined (set and not null
) value will be returned:
$val = $input1 ?? $input2 ?? $input3 ?? 'default';
Now that is a proper coalescing operator.