Home > Article > Backend Development > Reference counters and copy-on-write for php variables
As we all know, PHP does not support pointers, but what if you want two variables to point to the same memory block at the same time? In order to solve this problem, reference counters are used in the PHP kernel.
The previous blog post introduced how PHP variables are stored in the kernel. The following two member variables in the zval structure are used for reference counters:
<code>is_ref BOOL值,标识变量是否是引用集合。 refcount 计算指向引用集合的变量个数。 </code>
Look at the PHP code below
<code><?php $a = "this is a"; ?> </code>
An entity of a zval structure is called a zval container. Creating a variable in the PHP language layer will correspondingly create a zval container in the PHP kernel. Because the above code creates a variable $a, a zval container will be created in the PHP kernel; and because this variable is not a reference, the is_ref of the zval container is equal to FALSE, and the refcount is equal to 1.
Look at the code below
<code><?php $a = "this is a"; $b=$a; ?> </code>
The above code creates two variables
Copyright Disclaimer: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces the reference counter and copy-on-write of PHP variables, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.