“PHP中continue的使用”的版本间的差异
来自Blueidea
| 第9行: | 第9行: | ||
continue 接受一个可选的数字参数来决定跳过几重循环到循环结尾。 | continue 接受一个可选的数字参数来决定跳过几重循环到循环结尾。 | ||
| − | <?php | + | <source><?php |
while (list ($key, $value) = each($arr)) { | while (list ($key, $value) = each($arr)) { | ||
if (!($key % 2)) { // skip odd members | if (!($key % 2)) { // skip odd members | ||
| 第31行: | 第31行: | ||
} | } | ||
?> | ?> | ||
| − | + | </source> | |
省略 continue 后面的分号会导致混淆。以下例子示意了不应该这样做。 | 省略 continue 后面的分号会导致混淆。以下例子示意了不应该这样做。 | ||
2007-09-06T16:29:36的版本
switch,while等都是PHP非常常用的循环,而能够用好循环,对于使用PHP来编写有较强逻辑性的程序来说是非常不错的。 break是跳出循环,而continue是进行下一次循环。 很多东西手册上有,最重要的是如何把他们运用到实际当中去。
以下为摘抄的部分
continue 在循环结构用用来跳过本次循环中剩余的代码并在条件求值为真时开始执行下一次循环。
continue 接受一个可选的数字参数来决定跳过几重循环到循环结尾。
<source><?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";
}
?>
</source>
省略 continue 后面的分号会导致混淆。以下例子示意了不应该这样做。
<?php
for ($i = 0; $i < 5; ++$i) {
if ($i == 2)
continue
print "$i\n";
}
?>
希望得到的结果是:
0
1
3
4
可实际的输出是:
2