Zval is one of the most important data structures in PHP. It contains information about the value and type of variables in PHP.
1. zval
1.1 The structure of zval (zend_types.h)
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:
- zend_value: Save the value or pointer of a specific variable type
- The core function of u1 is to distinguish the type
- u2 is an auxiliary value
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:
- IS_UNDEF: The mark is undefined, indicating that the data can be overwritten or deleted. For example, when performing an unset operation on an array element, PHP 7 will not directly delete the data from the memory allocated to the HashTable. Instead, it will first mark the location of the Bucket where the element is located as IS_UNDEF. When the number of IS_UNDEF elements in the HashTable is When a certain threshold is reached, the elements marked by IS_UNDEF will be overwritten or deleted during the rehash operation.
- IS_TRUE and IS_FALSE: Here IS_BOOL is optimized into two, and the Boolean type tag is directly recorded in type.
- IS_REFERENCE: It is a new type. PHP7 uses different processing methods to handle "&"
- IS_INDIRECT: It is also a new type. Since the design of HashTable in PHP 7 is different from that of PHP5 There is a big difference between them, so to solve the problem of the global symbol table accessing the CV variable table, the IS_INDRECT type was introduced.
- IS_PTR: This type is defined as a pointer type, used to parse value.ptr, usually used on function types. For example, declare a function or method.
1.2 zend_value
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;
1.3 zval memory usage
In a zval:
- zend_value is union, occupying only 8 bytes. Just enough to store a zend_long, a double, or a pointer.
- u1 is 4 bytes and stores a v or type_info
- u2 is 4 bytes
So a zval is occupied 16 bytes. Correspondingly, in php5, the size of a zval is 48 bytes, which is indeed a huge improvement.
2. Variable storage
2.1 true, false, null
can be directly distinguished according to zval.u1.v.type without zend_value involvement
2.2 long, double
are stored directly in the lval or dval of zend_value.
2.3 Other types (string, array, object, resource, etc.)
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1
Easy-to-use and free code editor