A reference to the function returns
Look at the code first:
<?phpfunction &test () {static $b =0;//declares a static variable $b= $b +1;echo $b; return $b;} $a =test ();//This statement will output $b value of 1$a=5, $a =test ();//This statement will output a value of $b 2$a=&test ();//This statement will output the value of $b 3$a=5; $a =test ();//This statement outputs $ The value of B is 6?>
The following explanation:
In this way $a=test (); The resulting is not a function of the reference return, which is not the same as the normal function call, as for the reason: this is the PHP provisions, PHP rules through $a=&test (); The way to get the function is to return the reference, as to what is a reference to return it (the PHP manual says: Reference return is used when you want to use a function to find the reference should be bound to the top of the variable.) This nonsense, I did not understand the day.
In the example above, the explanation is
$a =test () Call the function, just assign the value of the function to $ A, and $ A does nothing to affect the $b in the function, and the function is called by $a =&test () , and his function is to return $b The memory address of the $b variable and the memory address of the $ A variable point to the same place, which produces the equivalent effect "$a =& $b;" So changing the value of $ A also changes the value of $b, so the
$a =&test (); $a = 5;
Later, the value of the $b becomes 5.
This is to let you understand that the function reference returns only use static variables, in fact, the function of the reference return is more used in the object
Also attached is a PHP official example:
This is the the-the-pointer to access variable inside the Class.<?phpclass talker{ private $data = ' Hi ';
public function & Get () { return $this->data; } Public function out () { echo $this->data; } } $AA = new talker (); $d = & $aa->get (); $aa->out (); $d = ' how '; $aa->out (); $d = ' is '; $aa->out (); $d = ' you '; $aa ->out ();? >//the output is "hihowareyou"
PHP references (&): A reference to a function returns