First, let's take a look at the following example:
<?php$a="val1";$b="val2";$a=&$b;echo $a."<br/>";$b="123";echo $a;?>
In the preceding example, If you replace $ A = & $ B with $ A = $ B, the two outputs of $ A have the same value, but with the & Symbol added, $ the memory addresses of B and $ a point to the same memory, therefore, if you change $ B $ A, it will also change! If the value of $ A changes, the output of $ B also changes accordingly!
Let's look at the example below.
<? Phpfunction & Test () {static $ B = 0; // declare a static variable $ B = $ B + 1; echo $ B; return $ B ;} $ A = test (); // This statement will output the value of $ B as 1 $ A = 5; $ A = test (); // This statement outputs the value of $ B as 2 $ A = & Test (); // This statement outputs the value of $ B as 3 $ A = 5; $ A = test (); // This statement outputs the value of $ B as 6?>
In this way, $ A = test (); does not actually return a function reference, which is no different from a common function call.
As for the reason: this is a PHP rule
PHP requires that $ A = & Test (); is used to obtain the function reference and return.
As for what is reference return (in the PHP manual, reference return is used when you want to use a function to find the variable on which the reference should be bound .)
The example above is as follows:
$ A = test () is used to call a function. It only assigns the value of the function to $ A. Any change made to $ A does not affect $ B in the function.
In the $ A = & Test () method, the function calls the memory address of the $ B Variable in return $ B and the memory address of the $ a variable,
Point to the same place. this is equivalent to the effect ($ A = & B;). Therefore, the value of $ A is changed and the value of $ B is changed. Therefore, the following code is executed:
$ A = & Test (); $ A = 5; then, the value of $ B is changed to 5.
Functions of variables and Functions & symbols