When defining a function, what is the meaning of the plus sign?
For example, please give an example. Thank you.
Best Answer [url = http://www.111cn.cn/bbs/space.php? Username = qxhy123] link tag qxhy123 [/url]
[Url = http://www.111cn.cn/bbs/space.php? Uid = 66514] link tag [img] http://www.111cn.cn/server/avatar.php? Uid = 66514 & size = small [/img] [/url] below I will reveal the correct answer. This is what I saw in the Forum's E-Snail shoes post, it accurately describes the role of adding & in front of the function and the specific effect.
Function reference return
First, copy the PHP content to the clipboard.
PHP code:
Function & test ()
{
Static $ B = 0; // declare a static variable
$ B = $ B + 1;
Echo $ B;
Return $ B;
}
$ A = test (); // This statement outputs the value of $ B as 1.
$ A = 5;
$ A = test (); // This statement outputs the value of $ B to 2.
$ A = & test (); // This statement outputs the value of $ B to 3.
$ A = 5;
$ A = test (); // This statement outputs a value of 6 for $ B.
The following explains:
In this way, $ a = test (); is not actually returned by the function reference, which is no different from the normal function call. The reason is: this is the 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 .) I haven't understood this sentence for a long time.
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.
But how to call a function through $ a = & test, the function is to direct the memory address of the $ B Variable in return $ B to the same place as the memory address of the $ a variable.
That is, the equivalent effect ($ a = & B;) is generated. Therefore, changing the value of $ a also changes the value of $ B.
$ A = & test ();
$ A = 5;
Later, the value of $ B is changed to 5.