參考功能
$name = 'alfa';
$address = 'street';
//declaring the function with the $ tells PHP that the function will
//return the reference to the value, and not the value itself
function &function_name($what){
//we need to access some previous declared variables
GLOBAL $name,$address;//or at function declaration (use) keyword
if ($what == 'name')
return $name;
else
return $address;
}
//now we "link" the $search variable and the $name one with the same value
$search =& function_name('name');
//we can use the result as value, not as reference too
$other_search = function_name('name');
//any change on this reference will affect the "$name" too
$search = 'new_name';
var_dump($search,$name,$other_search);
//will output string 'new_name' (length=8)string 'new_name' (length=8)string 'alfa' (length=4)
通常使用的方法與實施相同的接口對象,你要選擇你要的對象的函數與下一個合作。
通過引用傳遞:
function ($var, &$var2, $var3){...}
我敢肯定,你看到的例子,所以我就解釋如何以及何時使用它。 基本情況是,您何時需要應用於當前對象/數據的大邏輯,並且不希望在內存中製作更多副本。 希望這有助於。
看到它http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php'=&References'塊。 – Winston 2013-02-27 18:23:13