The break statement can also be used to jump out of a loop.
This example jumps out of the loop when x is equal to 4:
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
break;
}
echo "The number is: $x <br>";
}
?>
break
(PHP 4, PHP 5, PHP 7, PHP 8)
break
结束当前
for
,foreach
,while
,do-while
或者 switch
结构的执行。
break
可以接受一个可选的数字参数来决定跳出几重循环。
<?php
$arr = array('one', 'two', 'three', 'four', 'stop', 'five');
while (list (, $val) = each($arr)) {
if ($val == 'stop') {
break; /* You could also write 'break 1;' here. */
}
echo "$val<br />\n";
}
/* 使用可选参数 */
$i = 0;
while (++$i) {
switch ($i) {
case 5:
echo "At 5<br />\n";
break 1; /* 只退出 switch. */
case 10:
echo "At 10; quitting<br />\n";
break 2; /* 退出 switch 和 while 循环 */
default:
break;
}
}
?>
版本 | 说明 |
---|---|
5.4.0 |
break 0; 不再合法。这在之前的版本被解析为
break 1; 。
|
5.4.0 |
取消变量作为参数传递(例如 $num = 2; break $num; )。
|
The break statement can also be used to jump out of a loop.
This example jumps out of the loop when x is equal to 4:
<?php
for ($x = 0; $x < 10; $x++) {
if ($x == 4) {
break;
}
echo "The number is: $x <br>";
}
?>
You can also use break with parentheses: break(1);
Note:
Using more nesting level leads to fatal error:
<?php
while (true) {
foreach ([1, 2, 3] as $value) {
echo 'ok<br>';
break 3; // Fatal error: Cannot 'break' 3 levels
}
echo 'jamais exécuter';
break;
}
?>