PHP 继续语句

发布于 2024-08-11 05:07:15 字数 852 浏览 7 评论 0原文

当阅读一本 PHP 书籍时,我想尝试我自己的(继续)示例。 我编写了以下代码,但它不起作用,尽管一切似乎都正常

$num2 = 1;

    while ($num2 < 19)
        {
            if ($num2 == 15) { 
            continue; 
            } else {
            echo "Continue at 15 (".$num2.").<br />";
            $num2++;
            }

        }

输出是

Continue at 15 (1).
Continue at 15 (2).
Continue at 15 (3).
Continue at 15 (4).
Continue at 15 (5).
Continue at 15 (6).
Continue at 15 (7).
Continue at 15 (8).
Continue at 15 (9).
Continue at 15 (10).
Continue at 15 (11).
Continue at 15 (12).
Continue at 15 (13).
Continue at 15 (14).

Fatal error: Maximum execution time of 30 seconds exceeded in /var/www/php/continueandbreak.php on line 20

第 20 行是该行

if ($num2 == 15) { 

您能告诉我我的示例有什么问题吗? 我很抱歉提出这样的菜鸟问题

When reading a PHP book I wanted to try my own (continue) example.
I made the following code but it doesn't work although everything seems to be ok

$num2 = 1;

    while ($num2 < 19)
        {
            if ($num2 == 15) { 
            continue; 
            } else {
            echo "Continue at 15 (".$num2.").<br />";
            $num2++;
            }

        }

The output is

Continue at 15 (1).
Continue at 15 (2).
Continue at 15 (3).
Continue at 15 (4).
Continue at 15 (5).
Continue at 15 (6).
Continue at 15 (7).
Continue at 15 (8).
Continue at 15 (9).
Continue at 15 (10).
Continue at 15 (11).
Continue at 15 (12).
Continue at 15 (13).
Continue at 15 (14).

Fatal error: Maximum execution time of 30 seconds exceeded in /var/www/php/continueandbreak.php on line 20

Line 20 is that line

if ($num2 == 15) { 

Would you please tell me what's wrong with my example ?
I am sorry for such a Noob question

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

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

发布评论

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

评论(3

﹏半生如梦愿梦如真 2024-08-18 05:07:15

如果在 continue 之前不增加 $num2,您将陷入无限循环;

$num2 = 0;

while ($num2 < 18)
    {
            $num2++;
            if ($num2 == 15) { 
              continue; 
            } else {
              echo "Continue at 15 (".$num2.").<br />";
            }


    }

if you don't increment $num2 before the continue you will get into an infinite loop;

$num2 = 0;

while ($num2 < 18)
    {
            $num2++;
            if ($num2 == 15) { 
              continue; 
            } else {
              echo "Continue at 15 (".$num2.").<br />";
            }


    }
卸妝后依然美 2024-08-18 05:07:15

您甚至不需要继续那里,您的代码相当于;

$num2 = 1;
while ($num2 < 19){
    if ($num2 != 15) { 
        echo "Continue at 15 (".$num2.").<br />";
        $num2++;
    }
}

如果这不是您想要实现的目标,那么您使用的 continue 方法是错误的。

You don't even need continue there, your code equivalent to;

$num2 = 1;
while ($num2 < 19){
    if ($num2 != 15) { 
        echo "Continue at 15 (".$num2.").<br />";
        $num2++;
    }
}

If that's not what you're trying to achieve, you're using continue wrong.

三五鸿雁 2024-08-18 05:07:15

在 php 中,使用 foreach 表示数组,使用 for 表示循环

for($num = 1; $num < 19; $num++) {
    if ($num != 15) { 
       echo "Continue at 15 (" . $num . ") . <br />";
       break;
    }   
}

in php, use foreach for arrays and for for looping

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