gongzhonghao.png
扫码关注我们
最新赞助活动温馨提示:自愿赞助服务器费用,学生和没有工作的整站资源免费下载!
头像

php基础流程控制语句continue和break用法

来源:http://erdangjiade.com/topic/3016.html 你好,世界。 2017-09-25 22:42浏览(30)

continue
continue 在循环结构用用来跳过本次循环中剩余的代码并开始执行下一次循环。

注: 注意在 php教程 中 switch 语句被认为是作为 continue 目的的循环结构。

continue 接受一个可选的数字参数来决定跳过几重循环到循环结尾。

<?php 
while (list($key,$value) = each($arr)) 
{ 
    if ($key == "zhoz")
    { 
    // 如果查询到对象的值等于zhoz,这条记录就不会显示出来了。 
    continue; 
    } 
do_something ($value); 
}

例子2

<?php
foreach ($list as $temp) 
{ 
    if ($temp->value == "zhoz")
    { 
    continue; 
    // 如果查询到对象的值等于zhoz,这条记录就不会显示出来了。 
    } 
do_list; 
// 这里显示数组中的记录 
}

break
break 结束当前 for,foreach,while,do..while 或者 switch 结构的执行。

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

<?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";
}
/* using the optional argument. */
$i = 0;
while (++$i) {
   switch ($i) {
   case 5:
       echo "at 5<br>n";
       break 1;  /* exit only the switch. */
   case 10:
       echo "at 10; quitting<br>n";
       break 2;  /* exit the switch and the while. */
   default:
       break;
   }
}
?>

实例二

<?php 
$i = 0; 
while ($i < 7) 
{ 
    if ($arr[$i] == "stop") 
    { 
    break; 
    } 
$i++; 
} 
?>

以上就是php基础流程控制语句continue和break用法的详细内容,更多请关注二当家的素材网其它相关文章!

评论0
头像

友情提示:垃圾评论一律封号 加我微信:826096331拉你进VIP群学习群

1 2