Home  >  Article  >  Backend Development  >  php 怎么退出foreach 循环

php 怎么退出foreach 循环

WBOY
WBOYOriginal
2016-06-13 13:27:381677browse

php 如何退出foreach 循环
php 如何退出foreach 循环 break ;

------解决方案--------------------
break

如果是函数/方法处理结束,也可以用return

returnd的用法可以参考下面的文章,虽然说的是Java,但语法使用部分PHP同样适用。
简谈Java中的return
------解决方案--------------------

PHP code

foreach($data as $row)
{
   echo $row;
   break;
}
<br><font color="#e78608">------解决方案--------------------</font><br>
PHP code


foreach($array as $key => $value){
    if($value == 5)break;
}

// 这是一种。
// 如果是嵌套的循环,用continue加数字也可以实现

foreach($array as $key => $value){
    foreach($value as $key2 => $value2){
        if($value == 5)continue 2;
    }
}
<br><font color="#e78608">------解决方案--------------------</font><br>break ,continue,return, exit <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn