foreach 循环和 &$value 的引用 [英] foreach loop and reference of &$value
问题描述
为什么空的 foreach 循环可以改变结果.
我有以下代码:
$variable = [1,2,3,4];foreach ($variable as $key => &$value)$值++;var_dump($variable);
我得到的结果是:
数组(大小=4)0 =>输入 21 =>输入 32 =>整数 43 =>&int 5
现在,当我像这样添加一个空的 foreach 循环时:
$variable = [1,2,3,4];foreach ($variable as $key => &$value)$值++;foreach ($variable as $key => $value);var_dump($variable);
我明白了:
数组(大小=4)0 =>输入 21 =>输入 32 =>整数 43 =>&int 4
有人能解释一下为什么当我添加第二个空循环时最后一个元素没有改变,以及为什么有 &在最后一个元素之前?
在第一个循环结束时,$value
与 $variable[3]<指向同一个地方/code>(它们指向内存中的相同位置):
$variable = [1,2,3,4];foreach ($variable as $key => &$value)$值++;
即使这个循环结束了,$value
仍然是一个与 $variable[3]
指向相同内存位置的引用,所以每次你存储$value
中的一个值,这也会覆盖为 $variable[3]
存储的值:
foreach ($variable as $key => $value);var_dump($variable);
每次对这个 foreach 求值时,$value
和 $variable[3]
都变得等于 $variable 中可迭代项的值.
因此在第二个循环的第 3 次迭代中,$value
和 $variable[3]
通过引用变为等于 4,然后在第 4 次和最后一次迭代期间第二个循环,没有任何变化,因为您将 $variable[3]
(仍然是 &$value
)的值传递给 $value
(仍然是 &$value
).
这很令人困惑,但它甚至没有一点特殊性;它是代码执行完全,因为它应该.
此处的更多信息:PHP:通过引用传递><小时>
为了防止这种行为,在每个使用它的循环之后添加一个 unset($value);
语句就足够了.unset
的替代方法可能是将 foreach
循环包含在自调用闭包中,以强制 $value
是本地的,但是执行此操作所需的额外字符数量不仅仅是取消设置:
(function($variable){foreach ($variable as $key => &$value) $value++;})($变量);
Why is an empty foreach loop can change the result.
I have the following code:
$variable = [1,2,3,4];
foreach ($variable as $key => &$value)
$value ++;
var_dump($variable);
The result I get is:
array (size=4)
0 => int 2
1 => int 3
2 => int 4
3 => &int 5
Now, when I add an empty foreach loop like this:
$variable = [1,2,3,4];
foreach ($variable as $key => &$value)
$value ++;
foreach ($variable as $key => $value);
var_dump($variable);
I get this :
array (size=4)
0 => int 2
1 => int 3
2 => int 4
3 => &int 4
can someone explain me why the last element doesn't change when I add the second empty loop, and why there is a & infront of the last element?
At the end of the first loop, $value
is pointing to the same place as $variable[3]
(they are pointing to the same location in memory):
$variable = [1,2,3,4];
foreach ($variable as $key => &$value)
$value ++;
Even as this loop is finished, $value
is still a reference that's pointing to the same location in memory as $variable[3]
, so each time you store a value in $value
, this also overwrites the value stored for $variable[3]
:
foreach ($variable as $key => $value);
var_dump($variable);
With each evaluation of this foreach, both $value
and $variable[3]
are becoming equal to the value of the iterable item in $variable.
So in the 3rd iteration of the second loop, $value
and $variable[3]
become equal to 4 by reference, then during the 4th and final iteration of the second loop, nothing changes because you're passing the value of $variable[3]
(which is still &$value
) to $value
(which is still &$value
).
It's very confusing, but it's not even slightly idiosyncratic; it's the code executing exactly as it should.
More info here: PHP: Passing by Reference
To prevent this behavior it is sufficient to add an unset($value);
statement after each loop where it is used. An alternative to the unset
may be to enclose the foreach
loop in a self calling closure, in order to force $value
to be local, but the amount of additional characters needed to do that is bigger than just unsetting it:
(function($variable){
foreach ($variable as $key => &$value) $value++;
})($variable);
这篇关于foreach 循环和 &$value 的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!