WordPress企业主题定制/开发/优化

php break和continue语句

首页 » PHP » php break和continue语句

break
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";
}

/* 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;
}
}
?>

continue
continue 在循环结构用用来跳过本次循环中剩余的代码并开始执行下一次循环。

注: 注意在 PHP 中 switch 语句被认为是作为 continue 目的的循环结构。

continue 接受一个可选的数字参数来决定跳过几重循环到循环结尾。

<?php
while (list ($key, $value) = each ($arr)) {
if (!($key % 2)) { // skip odd members
continue;
}
do_something_odd ($value);
}

$i = 0;
while ($i++ < 5) {
echo "Outer<br>\n";
while (1) {
echo "&nbsp;&nbsp;Middle<br>\n";
while (1) {
echo "&nbsp;&nbsp;Inner<br>\n";
continue 3;
}
echo "This never gets output.<br>\n";
}
echo "Neither does this.<br>\n";
}
?>

分类与标签:

PHP

相关项目

  • WordPress外贸企业主题

  • 最近更新

  • 热门标签