问题描述
我一直将||
(双管道)用于if (($a == $b) || ($a == $c)) { }
,将or
用于do_this() or do_that();
.
I've always used ||
(double pipe) for if (($a == $b) || ($a == $c)) { }
and or
for do_this() or do_that();
.
为什么不是if (($a == $b) or ($a == $c)) { }
或do_this() || do_that();
?
使用这两个逻辑运算符中的任何一个是有原因的吗?还是仅出于个人喜好?
Is there a reason to use any of these two logical operators or it is just a personal preference?
&&
与and
的情况相同,我仅使用&&
.
The same applies for &&
vs. and
, of which I only use &&
.
推荐答案
拼写"运算符and
和or
的优先级较低,甚至低于赋值,因此您可以使用它们来避免写括号在很多地方例如:
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
等同于($d=$a||$b) && $c
它们在许多情况下也更具可读性.
They are also more readable in many contexts.
这篇关于逻辑运算符-“或"与"||" (双管道)在PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!