Php return and cancel references

Source: Internet
Author: User

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: [php] <? Php class foo {public $ value = 42; public function & getValue () {return $ this-> 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 shows a good understanding of [php] <? Php function & test () {static $ B = 0; // declare a static variable $ B = $ B + 1; echo $ B. "<br>"; return $ B;} $ a = test (); // the output value of $ B 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, therefore, the result is 1, 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! Unlike parameter transfer, the & symbol must be used here to indicate that a reference is returned instead of a normal copy. It also indicates that $ a is bound as a reference, instead of assigning values normally. Www.2cto.com Note: If you try to return the reference from the function like this: return ($ this-> value);, this will not work because you are 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 the result of a dynamic expression or new operator, an E_NOTICE error will be issued since PHP 4.4.0 and PHP 5.1.0. 2. Cancel the reference. When unset is a reference, the binding between the variable name and the variable content is closed. This does not mean that the variable content is destroyed. For example, [php] <? Php $ a = 1; $ B = & $ a; unset ($ a);?> Not unset $ B, just $. It may be helpful to understand this analogy with the Unix unlink call.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.