PHP 陷入无限循环
这段代码无限循环并给了我一个
致命错误:最大执行时间 超过 30 秒
这是我正在使用的代码
<?php
$sofar = 1;
while ($sofar == 1);
{
echo $sofar;
$sofar == $sofar+1;
}
?>
This code goes on a infinite loop and gives me a
Fatal error: Maximum execution time of
30 seconds exceeded
This is the code i am using
<?php
$sofar = 1;
while ($sofar == 1);
{
echo $sofar;
$sofar == $sofar+1;
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您的问题是使用两个等号来表示增量。即
$sofar = $sofar + 1
是正确的,但你有$sofar ==
。或者,只需$sofar++
或++$sofar
即可。基本上
这样做你的表达式将计算为
There for $sofar never cahnges, you必须使用
=
来更改或设置变量的值。您还可以在
while
语句末尾添加一个分号,分号表示 PHP 语句的结束。你应该做
Your problem is using two equal signs for the increment. Ie
$sofar = $sofar + 1
is correct but you have$sofar ==
instead. Alternatively just$sofar++
or++$sofar
works.your basically doing
so your expression would evaluate to
There for $sofar never cahnges, you have to use
=
to change or set the value of a variable.your also adding a semi-colon at the end of your
while
statement, The semicolon signifies the end of a PHP statement.You should be doing
你有一个 = 符号太多
并且你有一个 ;在你过了一段时间后。
一 = 符号赋值
两个 == 符号比较值
您还可以使用:
或者也许:
You have one = sign too many
And you have an ; after your while.
One = sign assign value
Two == signs compare values
You could also use:
Or perhaps:
是的,当然,它应该是:
而不是
后者(您正在使用)是一个条件语句。
Yes, definitely, it should be:
rather than
The latter one (which you are using) is a conditional statement.
您使用的
==
不是赋值运算符,而是条件运算符。您应该执行
$sofar = $sofar+1;
或$sofar++;
来增加值Your using
==
which is not an assignment operator but a conditional operators.you should be doing
$sofar = $sofar+1;
or$sofar++;
to increment the value==
是一个比较运算符 ,而不是分配运算符 (=
)这样指令$sofar == $sofar+1;
实际上没有做任何事情(它返回false
到任何地方)。换句话说:
$sofar
始终为1
。==
is a comparison operator, not assigment operator (=
) so that instruction$sofar == $sofar+1;
actually doesn't do anything (it returnsfalse
to nowhere).In other words:
$sofar
is always1
.while
语句末尾有一个分号。这相当于并且因此会导致无限循环。另外,您正在进行比较,而不是作业。您的代码应如下所示:
You have a semicolon at the end of your
while
statement. This is equivalent toand will therefore result in an infinite loop. Also, you are doing a comparison, not an assignment. Your code should look like this:
用
++
递增。Increment with
++
.