PHP foreach() 跳出本次循环与终止循环方法

admin 阅读:99 2024-03-04
$arr = array('a','b','c','d','e');
$html = '';
foreach($arr as $key => $value){
    if($value=='b'){
        $html .= $value;
        continue;
    }
   // 当 $value为b时,跳出本次循环,下面的代码不会被执行。
    if($value=='c'){
        $html .= $value;
        break;
    }
   // 当 $value为c时,终止整个循环,也就是foreach停止运行。
    $html .= $value;
}
echo $html; // 输出: ab

 

声明

1、部分文章来源于网络,仅作为参考。
2、如果网站中图片和文字侵犯了您的版权,请联系1943759704@qq.com处理!