PHP运算符差异&&和“和”[重复](PHP operator difference && and “and” [duplicate])

编程入门 行业动态 更新时间:2024-10-25 03:29:50
PHP运算符差异&&和“和”[重复](PHP operator difference && and “and” [duplicate])

可能重复: 'AND'和'&&'作为运算符

对不起,基本的问题,但我开始学习PHP只是一个星期前,并找不到在谷歌/ stackoverflow这个问题的答案。

我经历了下面的程序:

$one = true; $two = null; $a = isset($one) && isset($two); $b = isset($one) and isset($two); echo $a.'<br>'; echo $b;

它的输出是:

false true

我读&& /和是一样的。 两者的结果有何不同? 有人能说出真实的原因吗?

Possible Duplicate: 'AND' vs '&&' as operator

Sorry for very basic question but I started learning PHP just a week ago & couldn't find an answer to this question on google/stackoverflow.

I went through below program:

$one = true; $two = null; $a = isset($one) && isset($two); $b = isset($one) and isset($two); echo $a.'<br>'; echo $b;

Its output is:

false true

I read &&/and are same. How is the result different for both of them? Can someone tell the real reason please?

最满意答案

原因是运营商优先 。 在您使用&& and & =三个运算符中,优先顺序是

&& = and

因此,程序中的$b按照预期进行计算,但对于$b ,首先计算语句$b = isset($one) ,从而给出意想不到的结果。 它可以如下修复。

$b = (isset($one) and isset($two));

The reason is operator precedence. Among three operators you used &&, and & =, precedence order is

&& = and

So $a in your program calculated as expected but for $b, statement $b = isset($one) was calculated first, giving unexpected result. It can be fixed as follow.

$b = (isset($one) and isset($two));

更多推荐

本文发布于:2023-08-05 15:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1434072.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:运算符   差异   PHP   duplicate   difference

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!