How to assign first non false variable from a group of them
I tried this way without effect:
$a = false;
$b = false;
开发者_Python百科$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");
There's a couple of things going on here:
Firstly, in PHP the result of a boolean operation is a boolean.
Secondly, and more subtly, the "english" boolean operators (or
and and
) have a low precedence - lower than the assignment operator, =
.
Therefore in this expression $result
will always get the actual value of $a (regardless of $a
's value), since the assignment is applied before the boolean operator.
// This has the same effect:
$result = $a or $b or $c;
// As this:
$result = $a;
$a or $b or $c; // this has no effect
This is confusing, and almost certainly not what you want.
To get the boolean result of whether any of $a
, $b
, $c
is truthy (ie true
, or castable to true
) you can either use parentheses to force the precedence, or use the "C-style" operators (||
and &&
) which have a higher precedence:
// These all have the same effect:
$result = ($a or $b or $c);
$result = $a || $b || $c;
if ($a or $b or $c)
$result = true;
else
$result = false;
if ($a || $b || $c)
$result = true;
else
$result = false;
If you're unsure about operator precedence it's best to use parentheses - they also tend to help make code more readable, since the order of evaluation is made much more obvious.
It's also generally better to not rely on implicit type conversions (especially casting non-numeric strings), since it tends to make for unclear code.
edit:
To answer the actual question, another approach (though I don't really recommend it in this case, since you say you're only interested in the first non-false value) would be to use array_filter
without a callback - that would return an array of all the values in the input array that are truthy, with keys preserved.
eg:
$a = false;
$b = false;
$c = 'sometext';
$result = array_filter(array($a, $b, $c));
var_dump($result);
Outputs:
array(1) {
[2]=>
string(8) "sometext"
}
Result of a boolean operator is boolean in php.
$a = false;
$b = false;
$c = 'sometext';
$result = null;
foreach(array('a', 'b', 'c') as $k)
{
if($$k !== false)
{
$result = $$k;
break;
}
}
Also, consider moving your variables into an array.
精彩评论