Basic PHP reference count knowledge

Source: Internet
Author: User
A php variable is stored in a place called "zval". what does a zval structure contain? it contains the type and value of the variable, and two additional bitwise metadata, the first bit is called "is_ref", which is a Boolean value and identifies whether the variable is of reference type.

A php variable is stored in a place called "zval". what does a zval structure contain? it contains the type and value of the variable, and two additional bitwise metadata, the first bit is called "is_ref", which is a Boolean value and identifies whether the variable is a reference type, the PHP engine understands whether this variable is a common type variable or a reference type variable. Because php allows users to get a reference through the & operator. A zval container uses a mechanism called reference technology to optimize memory usage. The second two digits appended are called "refcount" and contain variable names (symbols) pointing to this "zval container. All the variable symbols in Php are saved in a place called the symbol table, and the period and range of each variable are saved. The range includes the complete cycle, or within each function or method.

When a variable is created using a constant value, a zval container is created. For example:

$ A = 'new string ';
?>

In the preceding example, a new symbolic name "a" is created in the current range (SCOPE) and the type is "string ", the new variable container with the value of "new string". because no reference has been set up by a user to point to it, the default value of "is_ref" is false, "refcount" is set to 1, indicating that only one symbol is used in this variable container. Note: If "refcount" is 1, "is_ref" is always "false". if you use xdebug, you can use it to view the response information:

Xdebug_debug_zval ('A ');
?>

The following information is displayed:

A: (refcount = 1, is_ref = 0) = 'new string'

Next, assign a value to another variable name to increase the reference count.

$ A = 'new string ';
$ B = $;
Xdebug_debug_zval ('A ');
?>

The following information is displayed:

A :( refcount = 2, is_ref = 0) = 'new string'

Here, refcount is 2, because the same variable container is connected to the symbols "a" and "B ", php is smart enough to determine whether to copy a variable without an actual variable container. when "refcount" is changed to 0, the variable container will be destroyed, when the variable symbol connected to the variable container leaves the scope (for example, the function ends) or calls unset () on the symbol table, "refcount" is reduced by 1, the following example illustrates this:

$ A = 'new string ';
$ C = $ B = $;
Xdebug_debug_zval ('A ');
Unset ($ B, $ c );
Xdebug_debug_zval ('A ');
?>

The following information is displayed:

A :( refcount = 3, is_ref = 0) = 'new sring'
A: (refcount = 1, is_ref = 0) = 'new string'

If we call "unset (a);", the variable container, including the internal values and types, will be removed from the memory.

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.