What is the difference between variable variables and reference values in php?

Source: Internet
Author: User
What is the difference between variable variables and reference values in php? In addition to the difference of & amp, are there any other differences? What are the differences between variable variables and Reference Assignment in php? I just started to learn php by myself. Please give me some answers.
Except for the difference, is there any other difference?

Reply content:

What is the difference between variable variables and reference values in php? I just started to learn php myself. Please give me some answers.
Except for the difference, is there any other difference?

From the perspective of the PHP kernel, you can understand it in depth;
PHP variables are stored in the kernel through the zval structure of the C language (you have never learned C, you can think of it as an object, and the member variables in it are class attributes, for now), The zval structure is as follows:
Struct _ zval_struct {
Zvalue_value value; // store the value of a variable.
Zend_uint refcount _ gc; // indicates that the reference count is 1 by default.
Zend_uchar type; // variable type
Zend_uchar is_ref _ gc; // indicates whether it is referenced.
};
For example, the php code is as follows:
$ A = 10;
$ B = $;
At this time, $ B is not referenced, but the value of $ a is assigned to $ a. Therefore, the refcount _ gc in the zval structure is changed to 2, while is_ref _ gc is false, indicates that it is not referenced, because kernel $ a has opened up a memory space. When $ a is assigned to $ B, the value of $ B only points to $, in this way, a new memory space is saved, but when the value of $ B is changed (the value of $ a does not change), $ B will open up a new memory space, this is the replication During writing. Here, the landlord has understood how to store PHP variables in the kernel. The following describes the reference:
$ A = 10;
$ B = & $;
At this time, the is_ref _ gc structure storing $ a in the kernel is marked as true, which indicates that it is a reference. Then, both $ a and $ B point to the same memory address, when $ B = 20, $ a will also change to 20. Here, the landlord should understand the difference, right?

The variable is created and referenced as an alias.

The variable completely opens another memory space to store data, which has nothing to do with the original value.
The reference uses the original value. If the referenced value changes, the original value changes.
You can take a look at the C language to understand the reference

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.