Home > Backend Development > PHP Tutorial > php中foreach跳出当前循环与终止循环方法

php中foreach跳出当前循环与终止循环方法

PHP中文网
Release: 2023-02-28 14:28:02
Original
2309 people have browsed it

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

<?php
$arr= array(&#39;le&#39;,&#39;yang&#39;,&#39;jun&#39;,&#39;lecode&#39;,&#39;gagade&#39;);  
$html= &#39;&#39;;  
foreach($arras $key => $value){  
    if($value==&#39;b&#39;){  
        $html.= $value;  
        continue;// 当 $value为b时,跳出本次循环  
    }  
    if($value==&#39;c&#39;){  
        $html.= $value;  
        break;// 当 $value为c时,终止循环  
    }  
    $html.= $value;  
}  
echo$html; // 输出: abc
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