Zval is one of the most important data structures in PHP. It contains information about the value and type of variables in PHP.
typedef struct _zval_struct zval;struct _zval_struct { zend_value value; /* value */ union { struct { ZEND_ENDIAN_LOHI_4( zend_uchar type, /* active type */ zend_uchar type_flags, zend_uchar const_flags, zend_uchar reserved) /* call info for EX(This) */ } v; uint32_t type_info; } u1; union { uint32_t var_flags; uint32_t next; /* hash collision chain */ uint32_t cache_slot; /* literal cache slot */ uint32_t lineno; /* line number (for ast nodes) */ uint32_t num_args; /* arguments number for EX(This) */ uint32_t fe_pos; /* foreach position */ uint32_t fe_iter_idx; /* foreach iterator index */ } u2;};
The zval structure is relatively simple and consists of three parts:
In order to look at zval more intuitively The structure and core field values are as shown in the picture above.
Explanation of u1.v.type:
The definition of zend_value in zval is as follows:
typedef union _zend_value { zend_long lval; /* long value */ double dval; /* double value */ zend_refcounted *counted; zend_string *str; zend_array *arr; zend_object *obj; zend_resource *res; zend_reference *ref; zend_ast_ref *ast; zval *zv; void *ptr; zend_class_entry *ce; zend_function *func; struct { uint32_t w1; uint32_t w2; } ww; } zend_value;
In a zval:
So a zval is occupied 16 bytes. Correspondingly, in php5, the size of a zval is 48 bytes, which is indeed a huge improvement.
can be directly distinguished according to zval.u1.v.type without zend_value involvement
are stored directly in the lval or dval of zend_value.
Use the pointer corresponding to zend_value to point to its specific structure.
For example, the structure of the string type is
struct _zend_string { zend_refcounted_h gc; zend_ulong h; /* hash value */ size_t len; char val[1]; };
The memory organization of a string variable is as shown in the figure below, zval.value.str points to the zend_string structure.
Zval is one of the most important data structures in PHP, which contains the values of variables in PHP and type-related information.
Recommended study: "PHP7 Tutorial"
The above is the detailed content of This article will take you to analyze the zval of php7. For more information, please follow other related articles on the PHP Chinese website!