按引用赋值的 PHP 误解 [英] Assign by reference PHP misunderstaning

查看:31
本文介绍了按引用赋值的 PHP 误解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面给出的代码中 $arr[0] 结果 "2" 的含义是什么,$arr2 正在复制 $arr 并将其第一个值加一,所以 $arr2[0] "2" 的结果是理解的,但是 $arr 发生了什么,当我通过引用 $arr[0] 传递给 $a 就像这样 $a=&$arr[0] $arr[0] 的结果是 2,当我通过值传递它时$a=$arr[0[ $arr[0] 的结果将被设置为 1,因为它应该,任何人都可以启发我吗?

Whats the meaning of $arr[0] result "2" in the code given below, $arr2 is copying $arr and increasing its first value by one, so the result of $arr2[0] "2" is understaning,but whats happening with $arr, when i pass by reference $arr[0] to $a like so $a=&$arr[0] the result of $arr[0] is 2, when i pass it by value $a=$arr[0[ the result of $arr[0] would be set to 1 as it should, can anyone enlighten me on this?

<?php
$a = 1;
$arr = array(1);
$a = &$arr[0];
$arr2 = $arr;
$arr2[0]++;
echo $arr[0]. "<br>";//2
echo $arr2[0]. "<br>";//2
?>

推荐答案

PHP 中的引用不像指针;当你通过引用分配或传递一些东西时,你创建了我们可能称之为引用集"的东西,其中两个变量都是对同一个zval"(内存中保存类型和值的结构)的引用一个变量).

References in PHP are not like pointers; when you assign or pass something by reference, you create what we might call a "reference set" where both variables are references to the same "zval" (the structure in memory which holds the type and value of a variable).

这样做的一个重要后果是引用是对称的.我倾向于通过引用将赋值写为 $foo =&$bar 而不是 $foo = &$bar 来强调这一点:运算符不只是引用 $bar 并将其放入 $foo,它影响两个操作数.

An important consequence of this is that references are symmetrical. I tend to write assign by reference as $foo =& $bar rather than $foo = &$bar to emphasise this: the operator doesn't just take a reference to $bar and put it into $foo, it affects both operands.

考虑到这一点,让我们来看看你的例子:

With that in mind, let's go through your example:

$arr = array(1);

这将创建两个 zval:一个用于数组 $arr 本身,另一个用于该数组的位置 0 ($arr[0]).

This will create two zvals: one for the array $arr itself, and one for the value in position 0 of that array ($arr[0]).

$a =& $arr[0];

这将 $a$arr[0] 绑定到一个引用集.无论我们使用哪个名称,我们都将使用之前创建的相同 zval,并且当前拥有 1.

This binds $a and $arr[0] into a reference set. Whichever of those names we use, we will be using the same zval that was previously created, and currently holds 1.

$arr2 = $arr;

这会将 $arr 的内容复制到一个新数组 zval 中,$arr2.但它不会将该数组中的引用解析为值,它只是复制它们是引用的事实.

This copies the contents of $arr into a new array zval, $arr2. But it doesn't resolve the references inside that array into values, it just copies the fact that they are a reference.

所以 $arr2[0] 现在也是包含 $arr[0]$a 的参考集的一部分.

So $arr2[0] is now also part of the reference set containing $arr[0] and $a.

$arr2[0]++;

这会增加我们的引用集指向的 zval,从 12.

This increments the zval pointed to by our reference set, from 1 to 2.

echo $arr[0]. "<br>";//2
echo $arr2[0]. "<br>";//2

因为它们都属于同一个引用集,所以它们都指向同一个 zval,即整数 2.

Since these are both part of the same reference set, they both point to the same zval, which is integer 2.

不可避免的问题是,错误还是功能?"一个更有用的例子是将包含引用的数组传递给函数:

The inevitable question is, "bug or feature?" A more useful example would be passing an array containing references into a function:

function foo($array) {
   $array[0] = 42;
   $array[1] = 69;
}
$a = 1;
$b = 1;
$foo = [ &$a, &$b ];
foo($foo);
echo $a; // 42
echo $b; // 69

与赋值一样,传入函数并不会破坏引用,因此操作它们的代码可以安全地重构为多个函数.

Like assignment, passing into the function didn't break the references, so code manipulating them can safely be re-factored into multiple functions.

这篇关于按引用赋值的 PHP 误解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆