在 foreach 循环中声明的 PHP 变量是否在每次迭代时被销毁和重新创建?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13626812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:48:30  来源:igfitidea点击:

Are PHP variables declared inside a foreach loop destroyed and re-created at each iteration?

phpvariablesloopsforeachscope

提问by Alexandre Bourlier

If I declare a variable inside a foreach loop, such as:

如果我在 foreach 循环中声明一个变量,例如:

foreach($myArray as $myData) {
    $myVariable = 'x';
}

Does PHP destroy it, and re-creates it at each iteration ? In other words, would it be smarter performance-wise to do:

PHP 是否销毁它,并在每次迭代时重新创建它?换句话说,在性能方面这样做是否更明智:

$myVariable;
foreach($myArray as $myData) {
    $myVariable = 'x';
}

Thank you in advance for your insights.

预先感谢您的见解。

回答by eisberg

In your first example:

在你的第一个例子中:

foreach($myArray as $myData) {
    $myVariable = 'x';
}

$myVariableis created during the first iteration and than overwritten on each further iteration. It will not be destroyed at any time before leaving the scope of your script, function, method, ...

$myVariable在第一次迭代期间创建,然后在每次进一步迭代时覆盖。在离开您的脚本、函数、方法等范围之前,它不会随时被销毁。

In your second example:

在你的第二个例子中:

$myVariable;
foreach($myArray as $myData) {
    $myVariable = 'x';
}

$myVariableis created before any iteration and set to null. During each iteration if will be overwritten. It will not be destroyed at any time before leaving the scope of your script, function, method, ...

$myVariable在任何迭代之前创建并设置为 null。在每次迭代期间 if 将被覆盖。在离开您的脚本、函数、方法等范围之前,它不会随时被销毁。

Update

更新

I missed to mention the main difference. If $myArrayis empty (count($myArray) === 0) $myVariablewill notbe created in your first example, but in your second it will with a value of null.

我没有提到主要区别。如果$myArray是空的(count($myArray) === 0$myVariable不会在你的第一个实例被创建,但在你的第二个它将为NULL值。

回答by Jeremy1026

According to the debugger in my IDE (NuSphere PHPed) in your first example:

根据您的第一个示例中我的 IDE (NuSphere PHPed) 中的调试器:

foreach($myArray as $myData) {
    $myVariable = 'x';
}

$myVariableis only created once.

$myVariable只创建一次。

回答by user4035

According to my experiment, it's the same:

根据我的实验,它是一样的:

<?php
for($i = 0; $i < 3; $i++) {
    $myVariable = $i;
}
var_dump($myVariable);

prints: int(2)

打印:int(2)

<?php
$myVariable;
for($i = 0; $i < 3; $i++) {
    $myVariable = $i;
}
var_dump($myVariable);

prints: int(2)

打印:int(2)

回答by user603749

The problem is $myVariable is not truly local to foreach only. So it can clobber a global variable under the same name.

问题是 $myVariable 并不是真正本地的 foreach。所以它可以破坏同名的全局变量。

A way around that is make your foreach an inline anonymous function.

一种解决方法是使您的 foreach 成为内联匿名函数。

E.g.

例如

$myforeach=function(&$myArray){ // pass by ref only if modifying it
  foreach($myArray as $myData) {
    $myVariable = 'x';
  }
};
$myforeach($myArray);  // execute anonymous.

This way you guarantee it will not step on other globals.

这样你就可以保证它不会踩到其他全局变量。