Reputation: 35
In my code I have this comparison:
$check_iter_C_sfavorito = ($tipo_iter_sfavorito[$id_loop][$k_tg] == 'C') AND (!(abs(@$delta_salto_check_sfavorito) < 0.001));
which result as true even when:
($tipo_iter_sfavorito[$id_loop][$k_tg] == 'C') ====>>>> is true
and
(!(abs(@$delta_salto_check_sfavorito) < 0.001)) ====>>>> is false
While it is OK if I use the operator "&&" instead of "AND", PHP operator precedence means this should not give this error, because of parentheses!
Thanks for the quick answers to both the "Mar* B*". I changed the code as follows:
$check_iter_C_sfavorito = (($tipo_iter_sfavorito[$id_loop][$k_tg] == 'C') AND (!(abs(@$delta_salto_check_sfavorito) < 0.001)));
... and it is OK!
Upvotes: 0
Views: 184
Reputation: 360592
It's directly specified in the documentation: http://php.net/manual/en/language.operators.precedence.php
and
and or
bind at LOWER precedence than =
, so your statement with and
is seen as
($foo = 'bar') AND 'baz';
Remember that in PHP, the result of an assignment is the value being assigned. This is how the standard "check for db failure" code works:
$result = mysql_query($sql) or die(msyql_error());
because or
binds lower than =
, the statement is parsed as:
($result = mysql_query($sql)) or die(mysql_error());
If the DB query fails, it returns false, which gets assigned to $result. The result of that assignment is also false, which gets or
ed with the die cal, causing the script to abort. If the query SUCCEEDS it returns a non-false value (the statement handle), and the or
never kicks in because of boolean shortcircuiting.
Upvotes: 1
Reputation: 212412
Equivalent of
($check_iter_C_sfavorito = ($tipo_iter_sfavorito[$id_loop][$k_tg] == 'C')) AND (!(abs(@$delta_salto_check_sfavorito) < 0.001));
because AND has lower precedence than the assignment (=)
Upvotes: 1