简体   繁体   English

PHP continue 语句

[英]PHP continue statement

When reading a PHP book I wanted to try my own (continue) example.在阅读 PHP 书籍时,我想尝试我自己的(继续)示例。 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第 20 行就是那条线

if ($num2 == 15) { 

Would you please tell me what's wrong with my example ?你能告诉我我的例子有什么问题吗? I am sorry for such a Noob question对于这样的菜鸟问题,我很抱歉

if you don't increment $num2 before the continue you will get into an infinite loop;如果在continue之前不增加$num2 ,您将进入无限循环;

$num2 = 0;

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


    }

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.如果这不是您想要实现的目标,那么您使用的是 continue 错误。

in php, use foreach for arrays and for for looping在PHP,使用的foreach为阵列和用于循环

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM