PHP 陷入无限循环

发布于 2024-10-10 21:52:47 字数 213 浏览 6 评论 0原文

这段代码无限循环并给了我一个

致命错误:最大执行时间 超过 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 技术交流群。

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

发布评论

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

评论(7

天涯离梦残月幽梦 2024-10-17 21:52:47

您的问题是使用两个等号来表示增量。即 $sofar = $sofar + 1 是正确的,但你有 $sofar == 。或者,只需 $sofar++++$sofar 即可。

基本上

if($sofar == $sofar+1){/*Nothing*/}

这样做你的表达式将计算为

if(1 == 2){/*nothing*/}

There for $sofar never cahnges, you必须使用 = 来更改或设置变量的值。

您还可以在 while 语句末尾添加一个分号,分号表示 PHP 语句的结束。

你应该做

if( condition )
{

}

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

if($sofar == $sofar+1){/*Nothing*/}

so your expression would evaluate to

if(1 == 2){/*nothing*/}

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

if( condition )
{

}
小梨窩很甜 2024-10-17 21:52:47
<?php 
$sofar = 1;

while ($sofar == 1)
{
echo $sofar;
$sofar = $sofar+1;
}

?>

你有一个 = 符号太多

并且你有一个 ;在你过了一段时间后。

一 = 符号赋值
两个 == 符号比较值

您还可以使用:

$sofar++;
$sofar += 1;
$sofar = $sofar +1;

或者也许:

$sofar = 1;

while ($sofar == 1)
{
    echo ++$sofar;
}
<?php 
$sofar = 1;

while ($sofar == 1)
{
echo $sofar;
$sofar = $sofar+1;
}

?>

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:

$sofar++;
$sofar += 1;
$sofar = $sofar +1;

Or perhaps:

$sofar = 1;

while ($sofar == 1)
{
    echo ++$sofar;
}
对不⑦ 2024-10-17 21:52:47

是的,当然,它应该是:

$sofar = $sofar + 1

而不是

$sofar == $sofar + 1

后者(您正在使用)是一个条件语句。

Yes, definitely, it should be:

$sofar = $sofar + 1

rather than

$sofar == $sofar + 1

The latter one (which you are using) is a conditional statement.

猥︴琐丶欲为 2024-10-17 21:52:47

您使用的 == 不是赋值运算符,而是条件运算符。

您应该执行 $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

清风不识月 2024-10-17 21:52:47

== 是一个比较运算符 ,而不是分配运算符 (= )这样指令 $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 returns false to nowhere).

In other words: $sofar is always 1.

甜尕妞 2024-10-17 21:52:47

while 语句末尾有一个分号。这相当于

while ($sofar == 1) {

}

并且因此会导致无限循环。另外,您正在进行比较,而不是作业。您的代码应如下所示:

<?php 
$sofar = 1;

while ($sofar == 1)
{
echo $sofar;
$sofar = $sofar+1;
}

?>

You have a semicolon at the end of your while statement. This is equivalent to

while ($sofar == 1) {

}

and will therefore result in an infinite loop. Also, you are doing a comparison, not an assignment. Your code should look like this:

<?php 
$sofar = 1;

while ($sofar == 1)
{
echo $sofar;
$sofar = $sofar+1;
}

?>
留蓝 2024-10-17 21:52:47
<?php 
$sofar = 1;

while ($sofar == 1) {
  echo $sofar;
  $sofar++;
}
?>

++ 递增。

<?php 
$sofar = 1;

while ($sofar == 1) {
  echo $sofar;
  $sofar++;
}
?>

Increment with ++.

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