AND [和] && 之间的区别

发布于 2024-11-30 14:23:50 字数 274 浏览 1 评论 0原文

鉴于以下陈述:

if($value && array_key_exists($value, $array)) {

         $hello['world'] = $value;

        }

最好使用逻辑运算符 AND 与 && 相对?

Given the statement:

if($value && array_key_exists($value, $array)) {

         $hello['world'] = $value;

        }

Would it be best to use the logical operator AND as opposed to &&?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

葬シ愛 2024-12-07 14:23:50

他们自己也做同样的事情。所以 a && b 的含义与 a 和 b 相同。但是,它们并不相同,因为 && 的优先级高于 and。有关更多信息,请参阅文档

此示例显示了差异:

// The result of the expression (false && true) is assigned to $e
// Acts like: ($e = (false && true))
$e = false && true;

// The constant false is assigned to $f and then true is ignored
// Acts like: (($f = false) and true)
$f = false and true;

On their own, they do exactly the same. So a && b means the same than a and b. But, they are not the same, as && has a higher precedence than and. See the docs for further information.

This example here shows the difference:

// The result of the expression (false && true) is assigned to $e
// Acts like: ($e = (false && true))
$e = false && true;

// The constant false is assigned to $f and then true is ignored
// Acts like: (($f = false) and true)
$f = false and true;
水波映月 2024-12-07 14:23:50

您提供的链接有一个注释:

“and”和“or”运算符有两种不同变体的原因是它们的操作优先级不同。 (请参阅运算符优先级。)

在您的情况下,由于它是唯一的运算符,因此取决于您,但它们并不完全相同

The link you provide has a note:

The reason for the two different variations of "and" and "or" operators is that they operate at different precedences. (See Operator Precedence.)

In your case, since it's the only operator, it is up to you, but they aren't exactly the same.

最舍不得你 2024-12-07 14:23:50

它们在条件语句中是相同的,但在条件赋值时要小心

// The result of the expression (true && false) is assigned to $g
// Acts like: ($g = (true && false))
$g = true && false;

// The constant true is assigned to $h and then false is ignored
// Acts like: (($h = true) and false)
$h = true and false;

并且

// The result of the expression (false || true) is assigned to $e
// Acts like: ($e = (false || true))
$e = false || true;

// The constant false is assigned to $f and then true is ignored
// Acts like: (($f = false) or true)
$f = false or true;

来自逻辑运算符 您链接到的手册。

They are the same in conditional statements, but be careful on conditional assignments:

// The result of the expression (true && false) is assigned to $g
// Acts like: ($g = (true && false))
$g = true && false;

// The constant true is assigned to $h and then false is ignored
// Acts like: (($h = true) and false)
$h = true and false;

and

// The result of the expression (false || true) is assigned to $e
// Acts like: ($e = (false || true))
$e = false || true;

// The constant false is assigned to $f and then true is ignored
// Acts like: (($f = false) or true)
$f = false or true;

From the Logical Operators manual you linked to.

£烟消云散 2024-12-07 14:23:50

唯一的拼写差异..使用 && 而不是 ANDand 是一个很好的做法..

only difference of spelling..its a good practice to use && instead of AND or and..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文