php 直接跳出嵌套循环

break 结束当前 forforeachwhiledo-while 或者 switch 结构的执行。

break 可以接受一个可选的数字参数来决定跳出几重循环。

 1 <?php
 2 $arr = array('one', 'two', 'three', 'four', 'stop', 'five');
 3 while (list (, $val) = each($arr)) {
 4     if ($val == 'stop') {
 5         break;    /* You could also write 'break 1;' here. */
 6     }
 7     echo "$val<br />\n";
 8 }
 9 
10 /* 使用可选参数 */
11 
12 $i = 0;
13 while (++$i) {
14     switch ($i) {
15     case 5:
16         echo "At 5<br />\n";
17         break 1;  /* 只退出 switch. */
18     case 10:
19         echo "At 10; quitting<br />\n";
20         break 2;  /* 退出 switch 和 while 循环 */
21     default:
22         break;
23     }
24 }
25 ?>