php foreach 跳出本次循环/当前循环与终止循环的方法

 PHP中用foreach()循环中,想要在循环的时候,当满足某个条件时,想要跳出本次循环继续执行下次循环,或者满足某个条件的时候,终止foreach()循环,分别会用到:continuebreak
 
$arr= array('le','yang','jun','code','life','a','b','c');
$html= '';
foreach($arr as $key => $value){
    if($value == 'a'){
      $html.= $value;
    }
    if($value =='b'){
        $html.= $value;
        continue;// 当 $value为b时,跳出本次循环
    }
    if($value =='c'){
        $html.= $value;
        break;// 当 $value为c时,终止循环
    }
    //$html.= $value;
}
echo $html; // 输出: abc

 

posted @ 2018-08-20 15:46  段佳伟  阅读(3392)  评论(0编辑  收藏  举报