「continue」

内容:continue,对就是你知道的那个continue。

更新日期:2020年09月11日
@IGNORECHANGE

“Note: In PHP the switch statement is considered a looping structure for the purposes of continue. continue behaves like break (when no arguments are passed). If a switch is inside a loop, continue 2 will continue with the next iteration of the outer loop.”

意思是:在PHP中,switch 语句被认为是可以使用 continue 的一种循环结构。如果continue没有参数,那它和break一样。如果switch在循环中,continue 2将继续外层循环的下一轮。

continue 后面可以跟一个可选的数字。数字的含义标识要跳过几层循环。默认为 1。Demo:

<?php

$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";
}
// 下面是输出:
// Outer<br />
// Middle<br />
// Inner<br />
// Outer<br />
// Middle<br />
// Inner<br />
// Outer<br />
// Middle<br />
// Inner<br />
// Outer<br />
// Middle<br />
// Inner<br />
// Outer<br />
// Middle<br />
// Inner<br />

continue 后面的分号(;)不能省略:

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

// 该Demo只会产生错误,属于 E_COMPILE_ERROR 错误。
// 但是低于5.4.0的版本,会输出 2 !!!!!

更新记录

Version Description

5.4.0 continue 0; 是无效的。而以前的版本被当作continue 1;处理。

5.4.0 不再支持参数传值(e.g., $num = 2; continue $num;)。