跳过循环与跳出循环
2016-01-11 / PHP / 11023 次围观 / 0 次吐槽 /continue 在循环结构用用来跳过本次循环中剩余的代码并在条件求值为真时开始执行下一次循环。
注意:continue接受一个可选的数字参数来决定跳过几重循环到循环结尾。默认值是1,即跳到当前循环末尾。
PHP
<?php
while (list ($key, $value) = each($arr)) {
if (!($key % 2)) {
continue;
}
do_something_odd($value);
}
$i = 0;
while ($i++ < 5) {
echo "Outer<br />\n";
while (1) {
echo "Middle<br />\n";
while (1) {
echo "Inner<br />\n";
continue 3;
}
echo "This never gets output.<br />\n";
}
echo "Neither does this.<br />\n";
}
?>
break 结束当前 for,foreach,while,do-while 或者 switch 结构的执行。
注意:reak 可以接受一个可选的数字参数来决定跳出几重循环。
PHP
<?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" ;
}
?>
Powered By Cheug's Blog
Copyright Cheug Rights Reserved.