I tried this way without effect:
$a = false;
$b = false;
$c = 'sometext';
$result = $a or $b or $c or exit('error: all variables are false');
and $result should be set to $c, but this gives value of bool(false)
instead.
What about:
$result = $a ?: $b ?: $c ?: exit('doh!');
($result = $a) || ($result = $b) || ($result = $c) || exit("no");
or if you want 0 and empty string etc. not count as false:
(($result = $a) !== false) || (($result = $b) !== false) || (($result = $c) !== false) || exit("no");
think about whether this is really readable. You could also use the old fashioned way:
if ($a !== false)
$result = $a;
elseif ($b !== false)
$result = $b;
elseif ($c !== false)
$result = $c;
else
exit("no");
Edit: Just in case you ever need something dynamic ;-).
foreach(array('a','b','c') as $key)
if (($result = $$key) !== false)
break;
if ($result === false)
exit("no");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With