使用 AND 与 &&在 for 循环中(与优先级无关?)

发布于 2024-08-19 17:45:23 字数 402 浏览 5 评论 0原文

为什么这段代码打印“Hello!”四次然后打印“1”:

<?php
for ($i=1 AND $blah=1; $i<5; $i++) echo("Hello!");
echo($blah);
?>

虽然这不会打印出“Hello!”完全然后打印“1”:

<?php
for ($i=1 && $blah=1; $i<5; $i++) echo("Hello!");
echo($blah);
?>

我知道 AND 和 &&有不同的优先级,但这似乎并不适用于此。我缺少什么? (我使用的是上面代码的变体,因为我将在 for 循环中使用 $blah,并且我想为其设置值)。 感谢您的帮助!

Why is it that this code prints "Hello!" four times and then prints "1":

<?php
for ($i=1 AND $blah=1; $i<5; $i++) echo("Hello!");
echo($blah);
?>

While this doesn't print out "Hello!" at all and then prints "1":

<?php
for ($i=1 && $blah=1; $i<5; $i++) echo("Hello!");
echo($blah);
?>

I know AND and && have different precedences, but that doesn't seem to apply here. What am I missing?
(I'm using a variant of the code above, since I will use $blah within the for loop, and I want to set the value for it).
Thanks for any help!

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

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

发布评论

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

评论(2

大姐,你呐 2024-08-26 17:45:23

@OP,请阅读文档。它解释了示例 #1 逻辑运算符下的差异

@OP, please read this doc. It explains the difference under Example #1 logical operators

野侃 2024-08-26 17:45:23

我怀疑 AND 或 &&就是您在这里寻找的。如果要在初始化表达式中同时执行$i=1$blah=1,则需要用逗号分隔它们:

for ($i=1, $blah=1; $i<5; $i++) echo("Hello!");

I doubt that either the AND or && are what you're looking for here. If you want to execute both $i=1 and $blah=1 in the initialization expression, you need to separate them with a comma:

for ($i=1, $blah=1; $i<5; $i++) echo("Hello!");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文