好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

php foreach怎么停止循环

php foreach停止循环的方法:1、通过continue跳出foreach本次循环;2、通过break终止foreach循环。

本文操作环境:windows7系统、PHP7.1版,DELL G3电脑

php foreach怎么停止循环?

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

foreach()循环中,想要在循环的时候,当满足某个条件时,想要跳出本次循环继续执行下次循环,或者满足某个条件的时候,终止foreach()循环,分别会用到:continue 与 break

PHP:

 foreach($arras $key => $value){  
    if($value=='b'){
   $html.= $value;  
   continue;// 当 $value为b时,跳出本次循环  
    }  
    if($value=='c'){  
   $html.= $value;  
   break;// 当 $value为c时,终止循环  
    }
}
echo$html; // 输出: abc

JavaScript:

var myerror = null;  
   try{  
  arr.forEach(function(el,index){  
 if (el==20) {  
console.log("try中遇到20,能退出吗?");//  
foreach.break=new Error("StopIteration");  
 }else{  
console.log(el);  
 }  
  });  
   }catch(e){  
  console.log(e.message);  
  if(e.message==="foreach is not defined") {  
 console.log("跳出来了?");//  
 return;  
  }else throw e;  
   }//可以跳出来

推荐学习:《PHP视频教程》

以上就是php foreach怎么停止循环的详细内容!

查看更多关于php foreach怎么停止循环的详细内容...

  阅读:60次