Although this is a very old post. Spare logic for falsification values ββcan be encoded as follows.
$var = $foo ?: $bar ?: "default";
In this case, when $foo is a falsified value (e.g. false, empty string, etc.), it returns to $bar , otherwise $foo . If the bar is a falsified value, it returns to the default line.
Keep in mind that this works with falsified values, and not only true .
example:
$foo = ""; $bar = null; $var = $foo ?: $bar ?: "default";
$var will contain the default text, because empty strings and null are considered "false" values.
[Update]
In php 7 you can use the new operator of union of zeros: ?? , which also checks if a variable exists using isset (). This is useful when you use a key in an array.
Example:
$array = []; $bar = null; $var = $array['foo'] ?? $bar ?? "default";
Prior to php 7, this would give an Undefined index: foo notification. But with a null union operator, this notification will not appear.
source share