Logical operators equation in php -
i have problem function should combine logical operators according data in array:
$arr = array( 0 => array(false, "or"), 1 => array(false, "or"), 2 => array(true) );
the equation should be:
- false or false or true
- ($arr[0][0] $arr[0][1] $arr[1][0] $arr[1][1] $arr[2][0])
and result: true
but wrong happens in function , returns false. missing?
var_dump( arrayboolvalidation($arr) ); function arrayboolvalidation (array $arr) { $num = count($arr); $status = $arr[0][0]; for($i = 1; $i < $num; ++$i) { if ($arr[$i-1][1] == "and") { $status = filter_var($status, filter_validate_boolean) , filter_var($arr[$i][0], filter_validate_boolean); } else if ($arr[$i-1][1] == "or") { $status = filter_var($status, filter_validate_boolean) or filter_var($arr[$i][0], filter_validate_boolean); } } return $status; }
it's operator precedence issue. and
not same &&
. @ http://php.net/manual/en/language.operators.precedence.php
=
has higher priority and
, $a = $b , $c;
equals $a = $b;
.
you must use brackets ($a = ($b , $c);
) or better use &&
. same thing or
(use ||
).
Comments
Post a Comment