将变量(数组)分配给其他变量不会从源变量中获取副本

时间:2018-05-29 13:18:45

标签: php arrays reference

假设代码如下:

$a = [1,2];
$b = &$a;     // $b holds reference to $a
$b[] = 3;     // so this makes $a to {1,2,3}
$b = $a;      // $b now holds COPY of $a (not reference)
$b[] = 4;     // so $a must be the same {1,2,3}
var_dump($a); // but it isn't - $a is {1,2,3,4}

所以某种语句$b = $a;并没有按原样分配$ a数组的新副本,而只是使用它的引用。那是为什么?

0 个答案:

没有答案