“&&”之间的区别和“并且” :运算符优先级和短路
我正在浏览 php.net 的运算符优先级部分,并遇到 this example 表示
$a = 1;
$b = null;
$c = isset($a) && isset($b);
$d = ( isset($a) and isset($b) );
$e = isset($a) and isset($b);
var_dump($a, $b, $c, $d, $e);
//Result:
int(1)
NULL
bool(false)
bool(false) <== I get this
bool(true) <== I do not get this
我在代码中使用了大量的调试和详细的 print(_r) 语句来跟踪我在代码中的位置。因此,我使用 $debug 和 print_r($dataArray)
或 $verbose 并打印“Updating dataArray\n”
作为代码之间的单独语句,使我能够控制这些打印(_r) 声明。这来自我的 BASH 经验,我曾经写过很多 [[ $condition ]] && { #如果为真则执行 } || { #Do if false }
。在 BASH 中,我知道它们是短路的,并利用这一事实编写了很多简单的单行代码。
现在,我观察到很多这种做法(编写 $verbose 和 print
)正在慢慢渗透到我的 if
语句中。我知道这不是推荐的做法,而且可能会伤到我的后背。然而,我确实想掌握这项技能,因为我喜欢写这样的衬里,并想将其用作我的个人风格。
所以我的问题是:
- 哪个运算符(
&&
或and
)短路了? - 该手册说
&&
优先于and
,但是有人可以通过将短路运算符特性/功能/特性与运算符优先级混合来举例说明这一点。 (基本上是优先级和短路的混合和匹配)
请详细说明运算符的短路和返回值性质。
PS:1.我希望这些运算符的关联性是相同且直观的,但如果您知道任何怪癖,请赐教。
PS:2.如果您仍然想警告我这种做法的危险,请附上例子。
编辑:通过用 ||
替换 or
更改我的简单 $var = mysql_(...) 或 die()
代码后,我发现使用 ||
和 &&
运算符而不是 and
和 or
是多么烦人的事情。 代码根本不起作用!据我了解,前一个构造将 TRUE
或 FALSE
的返回值分配给 $var
,这反过来又使 的所有顺序使用>$var
生成警告/错误/意外行为。后一个构造首先将 mysql_(...)
的结果分配给 $var
,然后计算 =
和 die< 的复合值。 /代码>。
这对我来说是一个很好的教训,我更好 1. 开始使用 PDO/mysqli 并自己处理错误 2. 在编写我上面称为个人风格的内容之前请三思。
//自我注意:不要在用另一种脚本/解释语言编写代码时使用一种脚本/解释语言的经验,每种语言都是独一无二的,并且有自己的怪癖和陷阱,这只是悲伤*叹息*
I was going through operator precedence section of php.net and came across this example which says
$a = 1;
$b = null;
$c = isset($a) && isset($b);
$d = ( isset($a) and isset($b) );
$e = isset($a) and isset($b);
var_dump($a, $b, $c, $d, $e);
//Result:
int(1)
NULL
bool(false)
bool(false) <== I get this
bool(true) <== I do not get this
I use quite a lot of debugging and verbose print(_r) statements in my code to keep track of where I am in the code. So I use $debug and print_r($dataArray)
or $verbose and print "Updating dataArray\n"
as separate statements in between the code, allowing me to control these print(_r) statements. This comes from my BASH experience where I used to write lot of [[ $condition ]] && { #Do if true } || { #Do if false }
. In BASH, I knew they are short circuited and used this fact to write lot of simple one liners.
Now I am observing that lot of this practice(of writing $verbose and print
) is slowly creeping into my if
statements. I am aware this is NOT a recommended practice and can bite me in the back. However, I do want to master this skill as I enjoy writing such one liners and want to use it as my personal style.
So my question(s) is(are) :
- Which operator (
&&
orand
) is short circuited ? - The manual says
&&
takes precedence overand
, but can someone exemplify this by mixing the short circuited operator feature/functionality/characteristic with operator precedence. (basically mix and match of precedence and short-circuiting)
Kindly elaborate on both the short-circuiting as well as return value nature of the operators.
PS: 1. I hope associativity of these operators is same and intuitive, but if you know any quirks, please enlighten me.
PS: 2. If you still feel like warning me against the perils of such practice, kindly include examples.
EDIT : After changing my simple $var = mysql_(...) or die()
code by replacing or
with ||
, I discovered how annoying it can be to use the ||
and &&
operators instead of and
and or
. The code simply didn't work ! To my understanding, the former construct assigns a return value of TRUE
or FALSE
to $var
, which in turn make all sequential use of $var
to generate warning/error/unexpected behavior. The latter construct assigns result of mysql_(...)
to $var
first and then evaluates the compound of =
and die
.
This is a good lesson for me, I better 1. Start using PDO/mysqli and handle errors on my own 2. Think twice before writing something I called above as personal style.
//note to self : don't use experience of one scripting/interpretive language while writing code in another, each one is unique and has its own quirks and pitfalls, and thats just sad *sigh*
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
代码
的解析方式与因此
$e
相同,由
isset($a)
和赋值确定,为true
- 与计算无关isset($b)
。The code
is parsed the same as
Therefore
$e
, as determined byisset($a)
and the assignment, istrue
- independent of evaluatingisset($b)
.