unset 只会取消引用,不会销毁空间
$a = 1;
$b = &$a;
unset($b);
echo $a."\n";
对象本身不会进行空间复制,若要空间复制,就使用clone,克隆对象
小伙伴可以自己试着解题
题目:
解题思路,把循环拆开来
$data = ['a','b','c'];
foreach ($data as $key => $val){
$val = &$data[$key];
}
$data = ['a','b','c'];
//第一次循环
$key = 0; $val = 'a';
$val = &$data[0] = 'a';
//循环结束
$data = ['a','b','c'];
//第二次循环
$key = 1; $val = 'b';
//因为 $val = &$data[0],所以$data[0] = b,$data = ['b','b','c']
$val = &$data[1] = 'b'
//第二次循环结束
$data = ['b','b','c'];
//第三次循环
$key = 2; $val = 'c';
//因为 $val = &$data[1],所以$data[1] = c,$data = ['b','c','c']
$val = &$data[2] = 'c'
//第三次循环结束
$data = ['b','c','c'];
最终 $data = ['b','c','c'];
空间图
验证结果
网友评论