PHP variables are stored in the zend Kernel. PHP variables can save any data type because they are weak type languages. However, php is written in C language, and C language is a strong type language. each variable has a fixed type and cannot change the type of the variable at will (you can change the type by force, but there may be problems.) in the zend Engine, how can a variable save any type?
In the zend/zend. h header file, the following struct is found:
typedef struct _zval_struct zval;typedef union _zvalue_value { long lval; double dval; struct { char *val; int len; }str; HashTable *ht; zend_object_value obj;} zvalue_value ;struct _zval_struct { zvalue_value value; zend_uint refcount; zend_uchar type; zend_uchar is_ref;};
The zval struct is a common expression of PHP variables in the kernel. In the zval struct, we can see four member variables:
Zvalue_value value; // The value of the variable. The value of the PHP variable is saved here zend_uint refcount; // The number of variable references. the variable references the calculator zend_uchar type; // The variable type zend_uchar is_ref; // whether the variable is referenced
The value Member variable of the zval struct is a zvalue_value consortium,PHP can maintain any structure type because of this consortium. From the member variables in the zvalue_value consortium, we can see that different types are saved to different member variables, so that PHP variables can store any data type. For example, if the variable is of the integer type, it is saved to the lval member variable of value; if the variable is of the string type, it is saved to the str member variable of value.
Another question is, how does the zend Engine know the type of the variable that is saved? We noticed that the zval struct contains a type member variable, which is the type of a php variable to be saved.
The zend Engine defines the variable type in 8:
#define IS_NULL 0#define IS_LONG 1#define IS_DOUBLE 2#define IS_STRING 3#define IS_ARRAY 4#define IS_OBJECT 5#define IS_BOOL 6#define IS_RESOURCE 7
Each macro defines a type corresponding to the php language layer. for example, when the type member variable of zval is equal to IS_STRING (zval. type = IS_STRING), the type of this variable is string.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.