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

php 怎么退出foreach 循环

WBOY
Release: 2016-06-13 13:27:38
Original
1698 people have browsed it

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>
Copy after login
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>
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template