PHP Reference and write-time replication

Source: Internet
Author: User

The PHP variable has been found to have REFCOUNT__GC (number of references) and IS_REF__GC (whether referenced) in the Zval struct

For example:

<?php

$a = "Hello World";

?>

At this point PHP will create a zval container because this variable is not a reference so the is_ref__gc of this container is false and REFCOUNT__GC is 1

And look at the code below.

<?php

$a = "Hello World";

$b = $a;

?>

Here because $b is not quoted $ A so here the Zval container is_ref__gc or false refcount__gc for 2 here we're sure to have doubts.

Why not quote REFCOUNT__GC and 2? This is due to PHP's write-time replication mechanism

<?php

$a = "Hello world";//The REFCOUNT__GC at this time is 1 is_ref__gc for 0

$b = $a;//The REFCOUNT__GC at this time is 2 is_ref__gc for 0

$a = "change";//The REFCOUNT__GC at this time is 1 is_ref__gc to 0

?>

When $ A is assigned to $b $a REFCOUNT__GC plus 1 is $ A $b is pointing to the same memory when $ A changes refcount__gc back to 1 because a memory block has been recreated to hold the new value, which is the copy-on-write mechanism

<?php

$a = "Hello world";//The REFCOUNT__GC at this time is 1 is_ref__gc for 0

$b =& $a;//refcount__gc at this time is 2 is_ref__gc for 1

$a = "change";//The REFCOUNT__GC at this time is 2 is_ref__gc to 1

?>

When a variable is shown to refer to another variable, refcount__gc is_ref__gc will add one to see how PHP determines when the variable is copied

If IS_REF__GC has a value (not 0) or refcount__gc <2 is 1, you can modify the variable so that it returns the entity of the variable directly instead of copying its value.

If IS_REF__GC is 2, changing the value of a variable will change the value of 2 variables because all 2 of these variables are a block of memory pointing to each other.

If refcount__gc <2 and is_ref__gc = 0 Modifies a value only changes the value of 1 variables and of course there's only one variable.

Rookie, write not good not to spray. Just to be able to turn over the nostalgia

PHP Reference and write-time replication

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.