logical operators or vs || (double pipe) in php [duplicate]

删除回忆录丶 提交于 2019-11-30 22:06:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!