gpt4 book ai didi

PHP 会更改循环的范围并在包含的文件中切换

转载 作者:行者123 更新时间:2023-12-04 10:59:48 24 4
gpt4 key购买 nike

如果在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com