php,while()循环中的无限循环
/// infinite loop??
$x=1;
while($x=9){
echo $x;
$x++;
}
我不明白背后的原因,为什么上面的代码会导致无限循环 在我看来,上面的代码应该输出“9”一次。但它首先输出无穷无尽的 999999999......
(当 x 等于 1 时) while 语句为 false 所以什么也没有发生, 然后 x 变为 2,但 while 语句再次为 false;
因此,当 x 变为 9 而语句为 true 时,它应该回显 9,然后我们由于 x++ 而加 1;它变成了 10,所以 while 语句变得错误,但据我所知它没有,因为
它继续回显 9999999.......
请就上面的代码启发我。此致。
注意:我已经检查过类似的问题,但找不到适合我的情况的答案,thx
/// infinite loop??
$x=1;
while($x=9){
echo $x;
$x++;
}
i dont understand the reason behind, why the above code causes infinite loop
in my opinion above code should output "9" once. but it outputs endless 999999999......
at first (when x is equal to 1) while statement is false so nothing happens,
then x becomes 2 but again while statement is false;
So when x becomes 9 while statement is true so it should echo 9 then we add 1 due to x++; and it becomes 10 so while statement becomes false but as i see it doesnt because
it continues to echo 9999999.......
pls enlighten me regarding the above code. best regards.
note:i have checked the similar questions but cant find the answer for my situation thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
$x=9
是一个赋值,并且始终为 true。也许您的意思是$x==9
或其他一些关系运算符。$x=9
is an assignment, and is always true. Perhaps you meant$x==9
, or some other relational operator.你的意思是
但在你的例子中它不会做任何事情,因为 $x != 9。你可能的意思是
You mean
But in your example it won't do anything, because $x != 9. You probably mean
您将值 9 赋给变量 x,而不是执行关系比较。一个常见的错误。 = 是赋值运算符,而 == 是相等比较运算符。
http://en.wikipedia.org/wiki/Assignment_(computer_science)#Assignment_versus_equality
You are assigning the value of 9 to the variable x instead of performing a relational comparison. A common mistake. = is the assignment operator whereas == is the equality comparison operator.
http://en.wikipedia.org/wiki/Assignment_(computer_science)#Assignment_versus_equality