可能重复:
参考 - 这个符号在PHP中意味着什么?
什么是&$variable
函数的含义和意义
function &SelectLimit( $sql, $nrows=-1, $offset=-1, $inputarr=false, $secs2cache=0 )
{
$rs =& $this->do_query( $sql, $offset, $nrows, $inputarr);
return $rs;
}
Run Code Online (Sandbox Code Playgroud)
像这样传递一个参数:myFunc(&$var);
意味着变量是通过引用传递的(而不是通过值传递).因此,对函数中的变量所做的任何修改都会修改调用所在的变量.
放在&
函数名之前表示"按引用返回".这有点非常直观.如果可能的话我会避免使用它.使用&符号启动PHP函数意味着什么?
要小心,不要与混淆&=
或&
运营商,这是完全不同的.
通过参考快速测试:
<?php
class myClass {
public $var;
}
function incrementVar($a) {
$a++;
}
function incrementVarRef(&$a) { // not deprecated
$a++;
}
function incrementObj($obj) {
$obj->var++;
}
$c = new myClass();
$c->var = 1;
$a = 1; incrementVar($a); echo "test1 $a\n";
$a = 1; incrementVar(&$a); echo "test2 $a\n"; // deprecated
$a = 1; incrementVarRef($a); echo "test3 $a\n";
incrementObj($c); echo "test4 $c->var\n";// notice that objects are
// always passed by reference
Run Code Online (Sandbox Code Playgroud)
输出:
Deprecated: Call-time pass-by-reference has been deprecated; If you would like
to pass it by reference, modify the declaration of incrementVar(). [...]
test1 1
test2 2
test3 2
test4 2
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12566 次 |
最近记录: |