Learning notes for passing values through variable references in php _ PHP Tutorial

Source: Internet
Author: User
Reference the variable in php to transfer the value learning notes. Reference: it means to pass the address of the original object in the memory to the target object, which means that the original object and the target object point to the same memory address. At this point, if you reference the target object: it means to pass the address of the original object in the memory to the target object, it is equivalent that the original object and the target object point to the same memory address. At this time, if you modify the target object or the original object, the data in the memory will also change.

The so-called value transfer means that only passing the object value to the target object is equivalent to copy; the system will re-open a completely identical memory space for the target object.

The so-called reference means passing the address of the object in the memory to the target object is equivalent to making the target object and the original object correspond to the same memory bucket. At this time, if you modify the target object, the data in the memory will also change.

If a value is passed, if it is not an object, a copy of the value will be sent, and any changes to this variable will not affect the original value. It refers to the memory address of the fax to transmit the reference or object. changes made to this variable will affect the original value.

The code is as follows:

Function func1 ($)

{$ A = $ a + 1 ;}

Function func2 (& $)

{$ A = $ a + 1 ;}

$ Sample = 1;

Func1 ($ sample );

Echo $ sample; // output 1

$ Sample = 1;

Func2 ($ sample );

9. echo $ sample; // output 2

Summary

In high school, the teacher told us that the reference is to give the memory address to the variable so that as long as the memory address changes, the variable content of our accounting will also change, this is a bit like a house with things. we can all go to get things. as long as things have changed, we can see the same things, rather than holding things in our hands, in this way, the things in the room have changed.

Bytes. In this case, if the target object is...

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.