Variable reference values in PHP learn notes

Source: Internet
Author: User

The so-called value pass, which means that only the value of the object is passed to the target object, which is equivalent to copy; The system will reopen a completely identical memory space for the target object.

A reference, that is, passing an object's address in memory to the target object is equivalent to having the target object and the original object correspond to the same memory storage space. In this case, the data in memory will change if the target object is modified.

If the value is not an object, a copy of the value will be transmitted, and any changes made to the variable do not affect the original value. Passing the reference or passing the object, is to pass the real memory address, changes to this variable will affect the original value.

The code is as follows Copy Code

function func1 ($a)

{$a = $a + 1;}

function Func2 (& $a)

{$a = $a + 1;}

$sample = 1;

Func1 ($sample);

Echo $sample; Output 1

$sample = 1;

Func2 ($sample);

9.echo $sample; Output 2

Summarize

In high school the teacher told us that the reference is to give the memory address to the variable so long as the memory address changes and the contents of our accounting variables will be changed, it's kind of like a house with something that we can all get into, as long as things change. We go in and see things the same, Instead of me holding things in my hand, things change in my room and I don't change.

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.