关于php循环跳出的问题_PHP
程序员文章站
2022-05-15 10:20:17
...
复制代码 代码如下:
//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层循环
//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层循环