Home >Backend Development >PHP Problem >What does PHP ZVAL mean?

What does PHP ZVAL mean?

coldplay.xixi
coldplay.xixiOriginal
2020-07-02 09:38:264487browse

PHP ZVAL is one of the most important data structures in PHP. It contains information about variable values ​​and types in PHP. It is a struct structure. The basic structure is [struct _zval_struct (zvalue_value value zend_uchar type)] .

What does PHP ZVAL mean?

##PHP ZVAL means:

The basic structure of ZVAL

Zval is one of the most important data structures in PHP (another important data structure is

hash table), which contains information about variable values ​​and types in PHP.

It is a struct, the basic structure is:

struct _zval_struct {
    zvalue_value value;     /* 存储变量的值*/
    zend_uint refcount__gc;  /* 表示引用计数 */
    zend_uchar type;          /* 变量具体的类型 */
    zend_uchar is_ref__gc;    /* 表示是否为引用 */
};
typedef struct _zval_struct zval;

Among them:

1, zval_value value

The actual value of the variable, specifically a union of

zvalue_value:

typedef union _zvalue_value {
    long lval;                  /* long value */
    double dval;                /* double value */
    struct {                    /* string */
        char *val;
        int len;
    } str;
    HashTable *ht;              /* hash table value,used for array */
    zend_object_value obj;      /* object */
} zvalue_value;

2, zend_uint refcount__gc

This value is actually a counter to save how many variables (or symbols, symbols, all symbols exist in the symbol table (symble table), different scopes use different symbol tables, we will discuss this later) points to the zval.

When a variable is generated, its refcount=1. A typical assignment operation such as a=b will increase the refcount of zval by 1, and the unset operation will decrease it by 1 accordingly. Before PHP5.3, the reference counting mechanism was used to implement GC. If the refcount of a zval was less than 0, then the Zend engine would think that there was no variable pointing to the zval, so it would release the memory space occupied by the zval.

But, sometimes things are not that simple. We will see later that the simple reference counting mechanism cannot GC the circularly referenced zval, even if the variable pointing to the zval has been unset, resulting in a memory leak (Memory Leak).

3. zend_uchar type

##This field is used to indicate the actual type of the variable. When we started learning PHP, we already knew that variables in PHP include four scalar classes (bool, int, float, string), two composite types (array, object) and two special types (resource and NULL) .

Inside zend, these types correspond to the following macros (code location

phpsrc/Zend/zend.h

): <pre class="brush:php;toolbar:false">#define IS_NULL 0 #define IS_LONG 1 #define IS_DOUBLE 2 #define IS_BOOL 3 #define IS_ARRAY 4 #define IS_OBJECT 5 #define IS_STRING 6 #define IS_RESOURCE 7 #define IS_CONSTANT 8 #define IS_CONSTANT_ARRAY 9 #define IS_CALLABLE 10</pre>

4.

is_ref__gc

This field is used to mark whether the variable is a reference variable. For ordinary variables, the value is 0, and for reference variables, the value is 1. This variable will affect the sharing, separation, etc. of zval. We will discuss this later.

As the name suggests,

ref_count__gc

and is_ref__gc are two very important fields required by PHP’s GC mechanism. The values ​​of these two fields can be passed Check with debugging tools such as xdebug.

Related learning recommendations:
PHP programming from entry to proficiency

The above is the detailed content of What does PHP ZVAL mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What does php yield mean?Next article:What does php yield mean?

Related articles

See more