break结束当前for, foreach, while, do-while或switch结构的执行。
break接受一个可选的数字参数,告诉它有多少个嵌套的封闭结构要被断开。默认值是1, 只有直接的包围结构被断开。但是数值不能大于嵌套的层数,否则会报错.
<?php
$arr = array('one', 'two', 'three', 'four', 'stop', 'five');
foreach ($arr as $val) {
if ($val == 'stop') {
break; /* You could also write 'break 1;' here. */
}
echo "$val<br />\n";
}
/* Using the optional argument. */
$i = 0;
while (++$i) {
switch ($i) {
case 5:
echo "At 5<br />\n";
break 1; /* Exit only the switch. */
case 10:
echo "At 10; quitting<br />\n";
break 2; /* Exit the switch and the while. */
default:
break;
}
}
?>
关键是 break 后面的参数比较牛逼!
