如果在 for 循环中存在一个包含文件,并且在该包含文件中存在另一个 for 循环,则 continue 2 不起作用
这是设计使然吗?或者这是一个错误? AM完全不知所措
信息如下
文件 temp_outer_loop.php 的内容
<pre>
<?php
for ($z=0; $z < 20 ; $z++) {
echo "\r\nlooping z $z of 20";
for ($a=0; $a < 20 ; $a++) {
echo "\r\n\tlooping a $a of 20";
if ($a==10) continue 2;
}
}
echo "<hr>";
for ($x=0; $x < 20 ; $x++) {
echo "\r\nlooping x $x of 20";
include_once(__DIR__ . "/temp_inner_loop.php");
}
?>
文件 temp_inner_loop.php 的内容
<?php
for ($y=0; $y < 20 ; $y++) {
echo "\r\n\tlooping y $y of 20";
if ($y==5) continue 2;
}
?>
最佳答案
我不相信这是一个错误。请参阅this post back in 2006在错误跟踪器上。它指出:
"include" in PHP is not "#include" in C. It doesn't just copy&paste your code. The included file has a separate execution context, even though it does inherit some things, most notably the symbol table from the including scope. You will also notice that writing "return" in the included file acts as a return from the file, not from the function it might be included in.
This kind of cross-file goto is definitely not intended to work.
关于PHP 会更改循环的范围并在包含的文件中进行切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58888774/