问题
I've always used ||
(double pipe) for if (($a == $b) || ($a == $c)) { }
and or
for do_this() or do_that();
.
Why not if (($a == $b) or ($a == $c)) { }
or do_this() || do_that();
?
Is there any reason to use any of these two logical operators or it is just a personal preference?
The same applies for &&
vs and
, which i only use &&
.
回答1:
The "spelled out" operators and
and or
have lower precedence, even lower than assignment, so you may use them to avoid having to write parentheses in so many places. For example:
$d=$a||$b and $c
is equivalent to ($d=$a||$b) && $c
They are also more readable in many contexts.
回答2:
The two different versions operate at different precedences:
http://www.php.net/manual/en/language.operators.precedence.php
来源:https://stackoverflow.com/questions/17710924/logical-operators-or-vs-double-pipe-in-php