“&"”的作用是什么?就在session变量之前


What is the function of "&" right before the session variable?

$cart = &$_SESSION['journal_items'];有人知道我们什么时候这样使用&符号吗?

基本上意味着$cart不会得到$_SESSION['journal_items']中存储的值,但它是引用。当你调用$cart时,你将调用$_SESSION['journal_items']的当前值,即使它在声明后被改变了。

的例子:

$_SESSION['journal_items'] = "test";
$cart = &$_SESSION['journal_items'];
//$cart's current value is "test"
$_SESSION['journal_items'] = "test2";
//$cart's current value is "test2"

请看答案和这本参考书