您的位置:首页 > 编程语言 > PHP开发

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

2016-01-18 11:19 696 查看

PHPforeach()跳出本次或当前循环与终止循环方法

PHP中用foreach()循环中,想要在循环的时候,当满足某个条件时,想

$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时,终止循环
}
$html .= $value;
}
echo $html; // 输出: ab


要跳出本次循环继续执行下次循环,或者满足某个条件的时候,终止foreach()循环,分别会用到:continue 与 break。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: