In-depth understanding of assignments and references in PHP

Source: Internet
Author: User

Original

Let's look at the following questions:

12345678 <?php$a= 10;//将常量值赋给变量,会为a分配内存空间 $b $a;//变量赋值给变量,是不是copy了一份副本,b也分配了内存空间呢? $c = &$a;//引用是不会为c分配空间的,c和a是共用一份空间的。?>

What is your answer to the question in the middle? Before today, my answer is to allocate memory space for B. Because that's what I understand:

& when assigning a value, consider a variable to define an alias, and add a reference to the memory space. Changing one of these will affect the other references. When using unset (), only a reference to the variable memory space is broken, and the memory space is not freed.

and = Assignment is different, it will re-open a memory space to store a copy of the original variable. The changes between the two do not affect each other.

This is confirmed by the following procedure:

123456789101112 <?php$a= 10;//将常量值赋给变量,会为a分配内存空间$b$a;//变量赋值给变量,是不是copy了一份副本,b也分配了内存空间呢?$c= &$a;//引用是不会为c分配空间的,c和a是共用一份空间的。$a= 5;echo$c;//输出5,因为a和c 是指向同一个内存空间echoPHP_EOL;echo$b;//由于b是副本,对a的操作不会影响b,输出10?>

What if

1 $b$a;//之后a  和  b 都不做任何改变,保持一致

There is such a problem, if = after the assignment, two variables have not changed, if it is two copies, is not too wasteful memory?

This is actually avoided in PHP.

  When you assign a variable to a new variable in PHP, the new variable is not immediately allocated a memory space, but a reference to the memory space is added. A new variable is allocated a memory space when the original variable or new variable makes any changes.

123456789 <?php $a  = 1; $b   $a  echo   $a //before this, B is shared with a memory space.   $a  = 2; //a made a change, at which point B will have its own space ?>

Each PHP variable exists in a variable container called "Zval". A Zval variable container that includes two bytes of extra information in addition to the type and value of the variable. The first is "Is_ref", which is a bool value that identifies whether the variable belongs to a reference collection (Referenceset). With this byte, the PHP engine can differentiate between normal and reference variables, and since PHP allows users to use the custom reference by using &, there is an internal reference counting mechanism in the Zval variable container to optimize memory usage. The second extra byte is "RefCount", which represents the number of variables (also known as symbols) that point to the Zval variable container. When the value of "RefCount" is 1 , the value of "Is_ref" is always FALSE .

After installing Xdebug, using Xdebug_debug_zval (), you can see the ZVAL structure:

As follows:

  

12345678910 <?php $a  = 1; $b   $a  echo   $a //before this, B is shared with a memory space. xdebug_debug_zval ( ' B ' $a  = 2; //a made a change, at which point B will have its own space xdebug_debug_zval ( ' B ' ?>

Output:

B:

(refcount=2, is_ref=0),

Int

1

B:

(Refcount=1, is_ref=0),

Int

1

As can be seen from the above results, the reference count is 2 before a change, and when a is changed, the reference count of B becomes 1 because B re-allocates space

In-depth understanding of assignments and references in PHP

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.