php在将两个元素数组作为by value参数传递时,数组的一个元素是如何通过引用的,而另一个元素是通过值?

发布于 2025-02-09 22:37:08 字数 773 浏览 2 评论 0原文

有人可以解释我在PHP 8.1.6中看到的这种奇怪的行为吗?

我想行为是按照标准的,我只是不明白。该函数调用具有按值按值的参数,但是在调用后,数组被更改,这都是因为对第一个元素的引用。

$arr = [3, 4];
$ref = &$arr[0] ;             // this statement causes the weird behaviour. Without it all is ok

print_r($arr);                // as expected
print_r(doSomethingTo($arr)); // as expected
print_r($arr);                // WHAT JUST HAPPENED?

function doSomethingTo($arr) {
    // $arr BY VALUE
    foreach($arr as $k => $v)
        $arr[$k]=$v+1 ;
    return $arr ;
}

结果:

Array
(
    [0] => 3
    [1] => 4
)
Array
(
    [0] => 4
    [1] => 5
)
Array
(
    [0] => 4
    [1] => 4
)

为什么?为什么对第一个数组元素的引用会改变按值参数传递的行为?现在,第一个元素是通过参考该函数传递的,第二个元素是按值进行的! ???

谢谢您的解释!

Can someone explain this strange behaviour I am witnessing in PHP 8.1.6?

I guess the behaviour is according to standards, I just don't understand it. The function call has a parameter by value, yet the array is changed after the call, all because of the reference to the first element.

$arr = [3, 4];
$ref = &$arr[0] ;             // this statement causes the weird behaviour. Without it all is ok

print_r($arr);                // as expected
print_r(doSomethingTo($arr)); // as expected
print_r($arr);                // WHAT JUST HAPPENED?

function doSomethingTo($arr) {
    // $arr BY VALUE
    foreach($arr as $k => $v)
        $arr[$k]=$v+1 ;
    return $arr ;
}

result:

Array
(
    [0] => 3
    [1] => 4
)
Array
(
    [0] => 4
    [1] => 5
)
Array
(
    [0] => 4
    [1] => 4
)

Why? Why does the reference to the first array element change the behaviour of the by value parameter passing? The first element was now passed by reference to the function, and the second by value! ???

Thank you for an explanation!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沦落红尘 2025-02-16 22:37:08

我在 https://www.php。 net/harual/en/enlaging.references.whatdo.php

我将把答案转换为这种特殊情况。

许多人可能没有意识到的是,$ a =& $ b并不意味着$ a引用现在对$ b,但是$ a和$ b都成为最初包含在$ b中的相同 value 的引用。

因此,当评估$ ref =& $ arr [0]; $ arr [0]元素和$ ref成为 引用3个值时。

现在,当数组作为参数按值传递时,始终重复数组。同样在这里也是如此。

这意味着$ arr [0] 参考也被复制,即在重复数组中另一个参考将创建到相同的3个值。因此,我们有$ ref,原始$ arr [0] 在函数 all>中的$ arr [0]是对3个值的引用。

当这三个参考文献中的任何一个都会分配时,价值当然会改变。

谢谢@sammitch将我设置在正确的轨道上!!!

A little complex formulated answer I found at https://www.php.net/manual/en/language.references.whatdo.php

I will translate the answer to this particular situation.

The thing many people may not realise, is, is that $a =& $b does not mean that $a references now to $b, but $a and $b BOTH become references to the same value that was originally contained in $b.

So when evaluating $ref = &$arr[0] ; the $arr[0] element and the $ref become both references to the 3 value.

Now when an array is passed as a parameter by value, the array is always duplicated. Same happens here.

Which means the $arr[0] reference is duplicated too, i.e. in the duplicate array another reference is created to the same 3 value. So we have $ref, the original $arr[0] and the $arr[0] within the function all being references to the 3 value.

When either of these three references gets an assignment, of course the value changes.

Thank you @Sammitch for setting me on the right track!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文