<?php// +----------------------------------------------------------------------// | Created by im-server.// +----------------------------------------------------------------------// | Language Reference >> References Explained >> What References Are Not// +----------------------------------------------------------------------// | Author: alexander <gt199899@gmail.com>// +----------------------------------------------------------------------// | Datetime: 2017-07-16 17:30// +----------------------------------------------------------------------// | Perfect Is Shit// +----------------------------------------------------------------------/** * 引用不是指標,所以不能實現case1中的情況; * case1 * case2 */namespace case1;function foo(&$var){ $var = &$GLOBALS["baz"]; return $var;}$baz = 0;$bar = 1;$foo = foo($bar);var_dump($foo);/** * 分析上面代碼 * 第一步:$bar通過引用傳遞到函數foo中的$var,所以$bar和$var指向同一個內容; * 第二步:函數foo中的$var又和$GLOBALS["baz"]指向同一個內容,此時$var的值變為0; * 原因為 * 輸出: * int(0) */namespace case2;function foo(&$var){ $GLOBALS["baz"] = &$var; return $var;}$baz = 0;$bar = 1;$foo = foo($bar);var_dump($baz);/** * 將$GLOBALS["baz"]和$var都指向了$var的內容,所以輸出為$bar的內容1; * 輸出: * int(1) */