Php reference return and unreference I. returning a reference is used when you want to use a function to find the variable to which the reference should be bound. Do not use return references to increase performance. the engine is smart enough for optimization. A reference is returned only when there are reasonable technical reasons! To return a reference, use this syntax: & lt ;? Phpclassfoo {php return and cancel references
I. return a reference
The return value of the reference is used when you want to use the function to find the variable on which the reference should be bound. Do not use return references to increase performance. the engine is smart enough for optimization. A reference is returned only when there are reasonable technical reasons! To return a reference, use this syntax:
value; }}$obj = new foo;$myValue = &$obj->getValue(); // $myValue is a reference to $obj->value, which is 42.$obj->value = 2;echo $myValue; // prints the new value of $obj->value, i.e. 2.?>
The above is the explanation provided by PHP Manual and it is easy to understand.
"; Return $ B ;}$ a = test (); // The Output $ B value is: 1 $ a = 5; $ a = test (); // the output value of $ B is: 2 $ a = & test (); // The output value of $ B is: 3 ** NOTE ** $ a = 5; // change the value of $ B to 5 $ a = test (); // The output value of $ B is: 6 ** NOTE **?>
$ A = test () although the return method is referenced when a function is defined, if a function is called in this normal situation, it serves the same purpose as a common function, so the result is 1 and 2.
$ A = & test () this call method is to reference and return, which is similar to $ a = & $ B. Then, in the second sentence, $ a = 5, that is, it equals to the variable $ B = 5, and the last sentence gets 6, which is easy to understand!
Different from passing parameters, it must be used in both places.&Symbol-indicates that a reference is returned instead of a normal copy.$Is used as a reference binding, rather than a normal value assignment.
Note: If you try to return a reference from the function like this:Return ($ this-> value );This will not work because it is trying to return the result of an expression rather than a referenced variable. Only variables can be referenced from function return-no other method. If the code tries to return a dynamic expression orNewThe result of the operator, which is sent from PHP 4.4.0 and PHP 5.1.0E_NOTICE
Error.
II. cancel referencing
When unset is a reference, it only disconnects the binding between the variable name and the variable content. This does not mean that the variable content is destroyed. For example:
Not unset$ B, Just$.
It may be helpful to understand this analogy with the Unix unlink call.