2011-07-14 155 views

回答

3

當您想通過引用分配變量時,使用=&。欲瞭解更多信息,請參閱http://php.net/manual/en/language.references.php

例子:

$a = array(1, 2, 3); 
// $b is a reference to $a. 
// If you change $a or $b, the value for both $a and $b will be changed. 
$b =& $a; 

$c = array(1, 2, 3); 
// $d is a copy of $c. 
// If you change $d, $c remains unchanged. 
$d = $c; 
+0

因此,這將類似於$$變量變量? –

+0

@Jae Choi - 編號只有當你想要使用'$$'時,如果你想動態引用一個變量。例如:'$ variable ='test';'你可以做'$$ variable',PHP會在'$ test'解釋它(它用它的值代替'$ variable'),但我不建議編碼辦法。這會讓你的代碼難以閱讀。 –

+0

謝謝弗朗索瓦! –

0
$b = 3; 
$a =& $b; 
$b = 5; //$a is 5 now 
$a = 7; //$b is 7 now