array(1) { [0]=> string(3) "yes" } } 运行以下代码: strin-6ren">
gpt4 book ai didi

PHP变量引用之谜

转载 作者:可可西里 更新时间:2023-11-01 13:26:15 26 4
gpt4 key购买 nike

运行以下代码:

<?php
$a = array('yes');
$a[] = $a;
var_dump($a);

输出:

array(2) {
[0]=>
string(3) "yes"
[1]=>
array(1) {
[0]=>
string(3) "yes"
}
}

运行以下代码:

<?php
$a = array('no');
$b = &$a;
$a[] = $b;
$a = array('yes');
$a[] = $a;
var_dump($a);

输出:

array(2) {
[0]=>
string(3) "yes"
[1]=>
array(2) {
[0]=>
string(3) "yes"
[1]=>
*RECURSION*
}
}

我已经为 $a 重新分配了值,为什么会有 RECURSION 循环引用?

最佳答案

要删除引用,您需要调用 unset。在 $a = array('yes'); 之后没有 unset $a 仍然与 $b 绑定(bind)并且它们仍然是引用。所以第二部分的行为与第一部分相同。

Note, however, that references inside arrays are potentially dangerous. Doing a normal (not by reference) assignment with a reference on the right side does not turn the left side into a reference, but references inside arrays are preserved in these normal assignments.

http://php.net/manual/en/language.references.whatdo.php

关于PHP变量引用之谜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26881871/

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