This article mainly introduces the decomposition of functions in php by adding symbols. The function is called reference return, which is a bit abstract. For more information, see this article. For more information, see below.
This article mainly introduces the decomposition of functions in php by adding symbols. The function is called reference return, which is a bit abstract. For more information, see this article. For more information, see below.
You don't need to talk about the meaning of adding the & Symbol in front of the php variable. Everyone is using it, that is, the two variables point to an address at the same time. So, what is the meaning of adding & symbol before the php function? Next, we will first demonstrate the code and then explain it again.
Function & chhua () {static $ B = "www.jb51.net"; // declare a static variable $ B = $ B. "WEB Development"; echo $ B; return $ B;} $ a = chhua (); // This statement outputs the value of $ B as "development" $ a = "PHP"; echo"
"; $ A = chhua (); // This sentence outputs the value of $ B as" development WEB development "echo"
"; $ A = & chhua (); // This statement outputs the value of $ B as" development WEB development "echo"
"; $ A =" JS "; $ a = chhua (); // This statement outputs the value of $ B as" JSWEB development "function & 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 a value of 6 for $ B.
Next, let's explain the second function.
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.