codecamp

PHP继续

PHP教程 - PHP继续

继续导致PHP跳过其余的当前循环迭代并继续下一次迭代。

例子

下面的代码使用continue语句来启动下一次迭代。


<?php
        for ($i = 1; $i < 10; $i = $i + 1) {
                if ($i == 3) continue;
                if ($i == 7) break;
                print "Number $i\n";
        }
?>

上面的代码生成以下结果。



实施例2

下面的代码显示了如何继续for循环。


<?php
    $usernames = array("grace","doris","gary","nate","missing","tom");
    for ($x=0; $x < count($usernames); $x++) {
        if ($usernames[$x] == "missing") 
           continue;
        echo "Staff member: $usernames[$x] <br />";
    }
?>

上面的代码生成以下结果。



实施例3

下面的代码显示了如何使用continue语句获取十个随机数,每个大于下一个。


<?php//from w  w w.  j ava 2  s .  c o  m
  //init variables
  $count = 0;
  $max = 0;

  //get ten random numbers
  while($count < 10)
  {
    $value = rand(1,100);

    //try again if $value is too small
    if($value < $max)
    {
      continue;
    }

    $count++;
    $max = $value;
    print("$value <br>\n");
  }
?>

上面的代码生成以下结果。

PHP break
PHP数组foreach循环
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

Operator

Introduction

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }