Reference in PHP4 and PHP5 &

Source: Internet
Author: User
Reference & amp; in PHP4 and PHP5 is reprinted from PHP in Chongqing. The original article address is http://www.php-chongqing.com/index.php/article/?php= operator (& amp ;), the reference in PHP means to access the content of the same variable with different names, the reference in PH PHP4 and PHP5 &
Article reprinted from Chongqing PHP, original address: http://www.php-chongqing.com/index.php/article/104
PHP provides the reference operator (&). the reference in PHP indicates that different names are used to access the content of the same variable. object references in PHP4 and PHP5 are different.
$a = 8;$b = $a;echo '$a:' . $a; // 8echo '$b:' . $b;// 8 $a = 12;echo '$a:' . $a; // 12echo '$b:' . $b;// 8 

First, create the variable $ a, assign the integer 8 to $ a, and then assign $ a to $ B. At this time, PHP creates a copy of $, then, assign the copy to $ B. In general, two variables are generated in the memory, with the variable values 8. one of the two variables points to $ a and the other to $ B, therefore, the printed results $ a and $ B are both 12. then, we assign 12 to $ a, and then print $ a and $ B. We can see that the value of $ a has changed to 12, the value of $ B is 8, which is easy to understand because $ a and $ B are completely irrelevant variables.


$a = 8;$b &= $a;echo '$a:' . $a; // 8echo '$b:' . $b;// 8 $a = 12;echo '$a:' . $a; // 12echo '$b:' . $b;// 12

First, create the variable $ a, assign the integer 8 to $ a, and then assign $ a to $ B. Note that $ a and $ B are printed using the & reference assignment function, the values of the two variables are 8. then, we assign 12 to $ a and print $ a and $ B. we can see that the value of $ B has also changed to 12. The reference operator (&) of PHP does not allow the program to generate a copy. $ a and $ B point to the same memory region, that is, $ a and $ B are the same variable, therefore, when the value of $ a changes, the value of $ B also changes. This is like 'Zhang San'. when I went to school, the students gave me the nickname 'sanwa '. whether it was 'Zhang San' or 'sanwa', it referred to more than one hundred catties, it refers to the same person.

In PHP4, when you create an object and assign it to other variables, PHP4 always generates a copy of the object and copies the entire object and all the content. This type of object processing method of PHP4 is very bad. copying an object will make the program occupy more memory, and copying an object will often cause some inexplicable errors. This bad object processing method has been improved in PHP5. in PHP5, objects are always transmitted in reference mode.
$user = new User();$user->name = 'zhangsan';$bing = $user;$bing->name = 'bing.peng';echo $user->name; // For PHP4: zhangsanFor PHP5: bing.peng

Run the above code in PHP4 and print the code 'hangsan'. in PHP5, the result is: 'Bing. peng'. you can see that PHP5 uses reference assignment by default. Note that PHP5 only uses reference assignment by default for object types. if the basic type still produces a copy, copy the object in PHP5, you need to use the clone keyword. to achieve the same effect in PHP4, we need to use the reference operator. the code is as follows:
$user = new User();$user->name = 'zhangsan';$bing &= $user;$bing->name = 'bing.peng';echo $user->name; 
Related Article

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.