长春师范大学政法学院:php流控制中的continue关键字

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/29 03:38:33
寻求一个范例解释带参数的continue关键字
要求改变参数后,运行的结果不同。

continue [parameter];

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 "  Middle<br />\n";
while (1) {
echo "  Inner<br />\n";
continue 3;
}
echo "This never gets output.<br />\n";
}
echo "Neither does this.<br />\n";
}
?>

省略 continue 后面的分号会导致混淆。以下例子示意了不应该这样做。

<?php
for ($i = 0; $i < 5; ++$i) {
if ($i == 2)
continue
print "$i\n";
}
?>

希望得到的结果是:

0
1
3
4

可实际的输出是:

2

因为 print() 调用的返回值是 int(1),看上去作为了上述可选的数字参数。
=========
上面从PHP手册里面摘录的,希望有帮助。