? An image of metaphor: Reference: You are my shadow, I wear what clothes you are also what clothes, you are unset, I am still me. Value: You are a photo of my time, or you are a separate thing, I say you are an apple, you are an apple, and I have no relationship. The following is reproduced: ? The PHP function in front of the & symbol means that the function of the reference back, the PHP function before the addition of the & symbol to what effect? PHP code?
function &test () { Static $b =0;//declaration of a statically variable $b = $b +1; Echo $b; return $b; } $a =test ();//This statement outputs a value of $b of 1 $a = 5; $a =test ();//This statement outputs a value of $b of 2 $a =&test ();//This statement outputs a value of $b of 3 $a = 5; $a =test ();//This statement will output a value of $b of 6? The following explanation: In this way $a=test () is not actually returned by a function reference, which is not the same as a normal function call. As for the reason: this is the PHP rule PHP rules through $a=&test (); The way to get is to return the reference to the function. As for what is a reference return (the PHP manual says that reference return is used when you want to use a function to find out which variable the reference should be bound to.) ) In the example above, the explanation is $a =test () call the function, just assign the value of the function to $ A, and no change to $ A will affect the $b in the function. And by calling the function by $a=&test (), his function is to return the memory address of the $b variable in the $b and the memory address of the $ A variable, Point to the same place. That produces the equivalent effect ($a =&b;) So change the value of $ A and change the value of $b at the same time, so in execution: $a =&test (); $a = 5; Later, the value of the $b becomes 5 ...
What does the & symbol in front of the PHP variable mean? Look at an example first PHP code? $foo = 321;??? $bar = & $foo;???? $bar = 123;??? Print $foo; $foo = 321; $bar = & $foo;? $bar = 123; print $foo; So what's the result of the output? PHP code? 123????
Why is that? Changing the new variable will affect the original variable, and this assignment is faster Note: Only named variables can pass address assignment That is, changing the value of the $bar will change the value of the $foo. |