function fun($pid=0,&$brr=array()){//Why does it not work if you remove &?
<code>$sql="select * from wuxianji where pid=$pid"; $rs=mysql_query($sql); while($arr=mysql_fetch_assoc($rs)){ $brr[]=$arr; fun($pid=$arr['id'],$brr); } return $brr;</code>
}
$brr=fun();
echo '
';print_r($brr);
die();//What does &$brr=array() & in function parameter passing mean? what's the effect?
Reply content:
function fun($pid=0,&$brr=array()){//Why does it not work if you remove &?
<code>$sql="select * from wuxianji where pid=$pid"; $rs=mysql_query($sql); while($arr=mysql_fetch_assoc($rs)){ $brr[]=$arr; fun($pid=$arr['id'],$brr); } return $brr;</code>Copy after loginCopy after login}
$brr=fun();
echo '';print_r($brr);<br>die();</p> <p>//What does &$brr=array() & in function parameter passing mean? what's the effect? </p> <p class="answer fmt" data-id="1020000006752366"> </p> <p>Look at the examples in the manual and you will understand&</p> <p class="answer fmt" data-id="1020000006752469"> </p> <p>Because & is a reference, when the & symbol is used, the modifications you make to the parameters in the function will also be carried outside the function. Without the & symbol, the modification of parameters you face in the function ends when the function ends. </p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><code>function a1($a) { $a = 20; } function a2(&$a) { $a = 20; } $a = 10; a1($a); var_dump($a); a2($a); var_dump($a);</code>Copy after login
Just like the above code, the output is 10, 20 in order. Because a2 uses the & symbol, the modification of $a in a2 also affects the $a actually passed to a2.
Infinitus classification requires an array that can be accumulated, similar to static. If the & is removed, the $array will become empty again every time it is recursed, and the data will not be accumulated!
& means that the address is passed, that is, any operation on the variable in the function will change the value of the external variable. Otherwise, when the function call ends, the changes in these variables will disappear and can only be changed by returning from the function