Questions about the separation and reference of PHP variables

Source: Internet
Author: User

Today, I look at my brother. The article about PHP variable separation and reference, there is a problem did not understand, first put the parts out:

If you follow the above, I'll change the code slightly as follows:


  
   

So

The second line of code:
$var _dup and $var point to the same zval, RefCount is 2.

When the third line is executed:
PHP discovers that the zval to be manipulated is more than 1 refcount, then PHP executes separation, separates $var_dup, and $var and $var_ref do the change on write association. That is, refcount=2, is_ref=1;

At the time of the fourth line:
Because $var and $var_ref point to the is_ref=1 of the zval, there is no separation so that the $var_ref and $var values are "OK".

In my understanding, the program at the end, because $var_dup in the execution of the third line when it has been separated out, so its value should remain "laruence" unchanged, but, I run the program when found that it has become "OK", which makes me very difficult to understand, I want to know the answer to the great God to help me. Do not know whether I understand the wrong or other hidden, thank you!

In addition are two small chestnuts for the great God Reference:


  
   

  
   

Reply content:

Today, I look at my brother. The article about PHP variable separation and reference, there is a problem did not understand, first put the parts out:

If you follow the above, I'll change the code slightly as follows:


  
   

So

The second line of code:
$var _dup and $var point to the same zval, RefCount is 2.

When the third line is executed:
PHP discovers that the zval to be manipulated is more than 1 refcount, then PHP executes separation, separates $var_dup, and $var and $var_ref do the change on write association. That is, refcount=2, is_ref=1;

At the time of the fourth line:
Because $var and $var_ref point to the is_ref=1 of the zval, there is no separation so that the $var_ref and $var values are "OK".

In my understanding, the program at the end, because $var_dup in the execution of the third line when it has been separated out, so its value should remain "laruence" unchanged, but, I run the program when found that it has become "OK", which makes me very difficult to understand, I want to know the answer to the great God to help me. Do not know whether I understand the wrong or other hidden, thank you!

In addition are two small chestnuts for the great God Reference:


  
   

  
   

Copy On Write!!!!!!!!!!!!
写时复制!!!!!!!!!!!!

Does the third line of your code have a write operation? Why do you need to perform the separation!?
The third line of action is to increase the refcount to 3.

  • 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.