[英]Php operators help needed
I'm from C++ background and newbie in php, somebody please help me with the difference between the two: 我来自C ++背景和php新手,请帮助我解决两者之间的区别:
$totalupdatedrows = (count($rows) == 1 and !isset($rows[0]->updated_by)) ? 0 : count($rows);
$totalupdatedrows = count($rows) == 1 and !isset($rows[0]->updated_by) ? 0 : count($rows);
assume count($rows) = 1 and $rows[0]->updated_by=null. 假设count($ rows)= 1且$ rows [0]-> updated_by = null。 Please see that first one has only one extra wrapper parentheses.
请注意,第一个只有一个额外的包装括号。
I'm getting correct result from the first statement. 我从第一条陈述中得到了正确的结果。 I expect to get result of 0 which is the result of the first one and not the second one.
我希望得到的结果为0,这是第一个而不是第二个的结果。 I can't figure out the difference.
我不知道有什么区别。
and
has lower precedence than the ternary operators ( ?
and :
). and
具有较低的优先级比所述三元运算符( ?
和:
)。 Therefore, you need to wrap it in parenthesis ( (
and )
) so it is evaluated first. 因此,您需要将其包装在括号(
(
和)
)中,以便首先对其进行评估。
Note that if you had used &&
, you would not need to use the extra parenthesis. 请注意,如果使用
&&
,则无需使用多余的括号。
Checkout the PHP documentation - Operator Precedence and
is nearly last. 查看PHP文档- 运算符优先级
and
并且几乎是最后一篇 。
If you change your and
to &&
it will sit at a higher order than ?:
如果将
and
更改为&&
,它将以比?:
高的顺序排列?:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.