引用变量

2018-12-20  本文已影响0人  Mraz_

定义:用不同的名字访问同一变量内容

$a = 1;
$b = $a;
$a = 1;
$b = &$a;
$data = ['a', 'b', 'c'];
foreach ($data as $key => $val) {
    $val = &$data[$key];
    print_r($data);
}

/*
loop_1
$key = 0
$val = 'a'
$val = &$data[0] =>'a' (此时$val与$data[0]指向同一地址)
$data = ['a','b','c'];

loop_2
$key = 1
$val = 'b' => $data[0] = 'b' ($data[0]与$val地址相同,$val改变,$data[0]的值也发生改变)
$val = &$data[1] => $val = 'b'($val地址再次发生改变,与$data[1]相同)
$data = ['b','b','c'];

loop_3
$key = 2
$val = 'c' => $data[1] = 'c'($data[1]与$val地址相同,$val改变,$data[1]的值也发生改变)
$val = &$data[2] => $val =>'c'($val地址再次发生改变,与$data[2]相同)
$data = ['b','c','c'];
*/
上一篇 下一篇

猜你喜欢

热点阅读