Cheug's Blog

当前位置:网站首页 / PHP / 正文

跳过循环与跳出循环

2016-01-11 / PHP / 10861 次围观 / 0 次吐槽 /

 continue   在循环结构用用来跳过本次循环中剩余的代码并在条件求值为真时开始执行下一次循环。

注意:continue接受一个可选的数字参数来决定跳过几重循环到循环结尾。默认值是1,即跳到当前循环末尾。

<?php
while (list ($key, $value) = each($arr)) {
    if (!($key % 2)) { 
        continue;
    }
    do_something_odd($value);

}

$i = 0;

while ($i++ < 5) {
    echo "Outer<br />\n";
    while (1) {
        echo "Middle<br />\n";
        while (1) {
           echo "Inner<br />\n";
           continue 3;
            }
        echo "This never gets output.<br />\n";
         }
    echo "Neither does this.<br />\n";
      }
?>

break 结束当前   forforeachwhiledo-while   或者 switch 结构的执行。

注意:reak 可以接受一个可选的数字参数来决定跳出几重循环。

<?php
$arr  = array('one','two','three','four','stop','five');
while (list (,  $val ) =  each ( $arr )) {    
       if ( $val  ==  'stop' ) {        
                break;     /* You could also write 'break 1;' here. */     
       }    
       echo  " $val <br />\n" ; 
    }
?>


额 本文暂时没人评论 来添加一个吧

发表评论

必填

选填

选填

必填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Cheug's Blog

Copyright Cheug Rights Reserved.