|
?
An image metaphor:
Reference: You are my shadow, my clothes, and your clothes. you are unset, and I am still me.
Pass value: You are a photo of me at a certain time, or you are an independent thing. I said you are an apple, and you are an apple. it has nothing to do with me.
Reprinted as follows:
?
The "&" symbol added before the php function indicates that the function is referenced and returned. what is the role of adding & symbol before the php function?
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 (); // The value of $ B is 6?
The following explains:
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; later, the value of $ B is changed to 5 ......
What is the meaning of adding the & symbol before the php variable?
First look at an example
Php code?
$ Foo = 321 ;???
$ Bar = & $ foo ;????
$ Bar = 123 ;???
Print $ foo;
$ Foo = 321;
$ Bar = & $ foo ;?
$ Bar = 123;
Print $ foo; what is the output result?
Php code?
123 ????
Why?
Changing new variables will affect the original variables. this assignment operation is faster.
Note: Only named variables can assign values to addresses.
That is to say, if the value of $ bar is changed, the value of $ foo is changed. |