Home  >  Article  >  php教程  >  php foreach/for循环跳出问题

php foreach/for循环跳出问题

WBOY
WBOYOriginal
2016-06-13 10:15:36832browse

在php 中for循环与foreach是常用的两个函数,for是用于数字较多,而foreach一般用于数组遍历中。

 代码如下 复制代码

 

//php当前循环为1,循环由里到外依次递增,break默认为1,例如跳出第2层循环
for ($i=0;$i     foreach (array(1,2,3) as $val){    
        foreach (array(1,2,3) as $val){         
            echo "1层循环
"; 
            break 2;  //跳出第2层循环        
        }
        echo "2层循环
";
    }
    echo "3层循环
";
}
//结果:
//1层循环
//3层循环
//1层循环
//3层循环
//1层循环
//3层循环

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