PHP break/continue语句

2023-10-18 136

一、break语句

在 PHP 循环结构中,break 用于结束当前循环,包括 for、foreach、while、do-while 和 switch。当满足某个条件时,执行 break 语句,程序将跳出当前循环,不再执行剩余的代码。

break 语句在循环结构中用于跳出当前循环,包括 for、foreach、while、do-while 和 switch。它可以接受一个可选的数字参数,表示要跳出的层数。默认值是1,break 只会跳出最近一层嵌套结构。

示例:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?<a href="https://www.zzbaike.com/tag/php" title="查看所有文章关于 php" target="_blank">php</a>
$arr = array('one', 'two', 'three', 'four', 'stop', 'five');
foreach ($arr as $val) {
if ($val == 'stop') {
break; /* 也可以在这里写 'break 1;'。 */
}
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;
}
}
?>
<?<a href="https://www.zzbaike.com/tag/php" title="查看所有文章关于 php" target="_blank">php</a> $arr = array('one', 'two', 'three', 'four', 'stop', 'five'); foreach ($arr as $val) { if ($val == 'stop') { break; /* 也可以在这里写 'break 1;'。 */ } 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; } } ?>
<?php
$arr = array('one', 'two', 'three', 'four', 'stop', 'five');
foreach ($arr as $val) {
if ($val == 'stop') {
break; /* 也可以在这里写 'break 1;'。 */
}
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;
}
}
?>

二、continue语句

continue 语句用于跳过本次循环中剩余的代码,并在条件求值为真时开始执行下一次循环。当满足某个条件时,执行 continue 语句,程序将跳过本次循环的剩余部分,直接进入下一次循环。

注意

  • 在 PHP 中 switch 语句被认为是可以使用 continue 的一种循环结构。
  • continue 的行为类似于没有传递参数的 break ,但会引发警告,因为这可能是一个错误。
  • 如果 switch 在循环内, continue 2 将会外部循环中的下一个迭代中继续。

continue 接受一个可选的数字参数来决定跳过几重循环到循环结尾。默认值是 1,即跳到当前循环末尾。

示例:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
$arr = ['zero', 'one', 'two', 'three', 'four', 'five', 'six'];
foreach ($arr as $key => $value) {
if (0 === ($key % 2)) { // 跳过偶数键的成员
continue;
}
echo $value . "\n";
}
?>
<?php $arr = ['zero', 'one', 'two', 'three', 'four', 'five', 'six']; foreach ($arr as $key => $value) { if (0 === ($key % 2)) { // 跳过偶数键的成员 continue; } echo $value . "\n"; } ?>
<?php
$arr = ['zero', 'one', 'two', 'three', 'four', 'five', 'six'];
foreach ($arr as $key => $value) {
if (0 === ($key % 2)) { // 跳过偶数键的成员
continue;
}
echo $value . "\n";
}
?>

以上示例会输出:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
one
three
five
one three five
one
three
five

示例:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
$i = 0;
while ($i++ < 5) {
echo "Outer\n";
while (1) {
echo "Middle\n";
while (1) {
echo "Inner\n";
continue 3;
}
echo "This never gets output.\n";
}
echo "Neither does this.\n";
}
?>
<?php $i = 0; while ($i++ < 5) { echo "Outer\n"; while (1) { echo "Middle\n"; while (1) { echo "Inner\n"; continue 3; } echo "This never gets output.\n"; } echo "Neither does this.\n"; } ?>
<?php
$i = 0;
while ($i++ < 5) {
echo "Outer\n";
while (1) {
echo "Middle\n";
while (1) {
echo "Inner\n";
continue 3;
}
echo "This never gets output.\n";
}
echo "Neither does this.\n";
}
?>

以上示例会输出:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Outer
Middle
Inner
Outer
Middle
Inner
Outer
Middle
Inner
Outer
Middle
Inner
Outer
Middle
Inner
Outer Middle Inner Outer Middle Inner Outer Middle Inner Outer Middle Inner Outer Middle Inner
Outer
Middle
Inner
Outer
Middle
Inner
Outer
Middle
Inner
Outer
Middle
Inner
Outer
Middle
Inner

不可省略 continue 后面的分号,否则会导致混淆,示例:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
for ($i = 0; $i < 5; ++$i) {
if ($i == 2)
continue
print "$i\n";
}
?>
<?php for ($i = 0; $i < 5; ++$i) { if ($i == 2) continue print "$i\n"; } ?>
<?php
for ($i = 0; $i < 5; ++$i) {
if ($i == 2)
continue
print "$i\n";
}
?>

希望得到的结果是:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
0
1
3
4
0 1 3 4
0
1
3
4
  • 广告合作

  • QQ群号:4114653

温馨提示:
1、本网站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。邮箱:2942802716#qq.com(#改为@)。 2、本站原创内容未经允许不得转裁,转载请注明出处“站长百科”和原文地址。
PHP break/continue语句
上一篇: PHP foreach语句
PHP break/continue语句
下一篇: PHP switc语句