在每种编程语言逻辑中都少不了,操作一些轮回来查找或比拟一些数据,但当我们在轮回未执行完毕之前假如找出了本身想要的功效,该假如跳出并竣事轮回呢?
我们就以PHP编程语言中的foreach轮回来说说,如何跳出本次/当前轮回与终止轮回的要领
PHP中阻止代码运行的语句
continue:跳出本次轮回
break:终止轮回
exit:用来竣事措施执行
return: 用来竣事一段代码
php中foreach跳出本次/当前轮回与终止轮回要领
示例代码:

$array= array('1','2','3','4','5');
$str= '';
foreach($array as $key => $value){
if($value == '2'){
$str .= $value;
continue;// 当 $value为2时,跳出本次轮回
}
if($value == '4'){
$str .= $value;
break;// 当 $value为3时,终止轮回
}
$str .= $value;
}
echo $str;
输出功效为:1234
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:https://www.heiqu.com/9192.html