Brief analysis of PHP shallow copy and deep copy

Source: Internet
Author: User

Preface:

in PHP, "=" is an assignment symbol, a deep copy for a normal variable, and a shallow copy for an object (the assignment of an object is a reference assignment).
Note: When an object is passed as a parameter, it is also a reference pass, regardless of whether the function defines a & symbol before the argument.


Briefly:

* Deep Copy: Assignment value is fully copied, full copy, change to one of them, does not affect the other. (Has been imitated, can also surpass.) * Shallow copy: When assigned, the reference assignment is equivalent to taking an alias. One of the modifications will affect the other. (Destiny Community, a proud, a loss of loss)


Practice:


1. Realization of the deep copy of common variables

The realization of the deep copy of the common variable $a= ' Zhangsan '; $b = $a; echo $a. " \ r \ n "Echo $b." \ r \ n "; $b =" Lisi "; Echo $a." \ r \ n "Echo $b." \ r \ n ";/** * Output Results *zhangsan *zhangsan * *zhangsan *lisi **/

Conclusion: The assignment of a normal variable is a deep copy


2. realization of shallow copy of common variable

The realization of shallow copy of ordinary variable $c= "Wangwu"; $d =& $c; Echo $c. " \ r \ n "Echo $d." \ r \ n "; echo" \ r \ \ "$d =" xiaoer "; Echo $c." \ r \ n "Echo $d." \ r \ n ";/** output Results *wangwu *wangwu * *xiaoer *xiaoer **/

Conclusion: The assignment of a normal variable is a deep copy







Brief analysis of PHP shallow copy and deep copy

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.