PHP returns the reference type. php returns the reference type.

Source: Internet
Author: User

PHP returns the reference type. php returns the reference type.

Return reference, which cannot be omitted during definition and call.

This is a confusing concept. If it is not for some special reasons, we recommend that you do not use it.

He is easy to mix because of the changes in the Reference in PHP5, resulting in a large difference in the behavior of PHP4/PHP5.

For example:
Copy codeThe Code is as follows:
<? Php
 
$ Color = 'yellowgreen ';
 
Function & getRef (){
Global $ color;
Return $ color;
}
 
Function getCopy (){
Global $ color;
Return $ color;
}
 
$ ColorRef = & getRef ();
$ ColorRef = 'blue ';
 
// $ ColorCopy = getCopy ();
// $ ColorCopy = 'black ';
 
 
Var_dump ($ color );
Var_dump ($ colorRef );
// Var_dump ($ colorCopy );
 

Run the above Code and you will understand it. Use & getRef () to bind $ colorRef to $ color, that is to say, the variables $ colorRef and $ color point to the same value. When one value is changed, the other value also changes.

If $ color is an array, the above Code performs the same in any version of PHP4/5.

When $ color is an Object, the problem occurs.

In PHP4, getCopy still returns a Copied Object; & getRef () returns a reference.

PHP5 is a little different,
For the latest PHP5.2, the behavior is the same as that for PHP4. getCopy () returns the copy and & getRef () returns the reference.
However, some versions of PHP5 are slightly different here. For example, PHP5.1.6 indicates that both getCopy () and & getRef () are referenced.

Here, getCopy () and & getRef () are two global functions. If they are put into the class as the class's Member functions, it will be another scene ......

PS: Returning References does not improve the performance, but sometimes reduces the performance. Therefore, do not use this feature to "optimize" the program.

If you have any questions, you can find the answer most of the time in the manual:

Http://cn.php.net/manual/en/language.references.php

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.