PHP is a weak scripting language. The weak type does not indicate that PHP variables have no type distinction. php variables have eight primitive types:
Four scalar types:
Boolean (Boolean)
Integer)
Float (float type)
String (string)
Two composite types:
Two special types:
If a variable can be converted from one type to another during runtime, how does PHP implement the type magic of this variable?
In the engine, all variables are represented by a struct, which can be found in {phpsrc}/Zend. h:
Struct _ zval_struct {<br/>/* variable information */<br/> zvalue_value value;/* value */<br/> zend_uint refcount _ GC; <br/> zend_uchar type;/* active type */<br/> zend_uchar is_ref _ GC; <br/> };
Here, we only care about the two members of value and type, where value is a union, that is, the actual value of the variable, and type is the dynamic type of the variable, depending on the type, value uses different members.
Various types of type are defined as macros, which are also defined in this file:
# Define is_null0 <br/> # define is_long1 <br/> # define is_double2 <br/> # define is_bool3 <br/> # define is_array4 <br/> # define is_object5 <br /># define is_string6 <br/> # define is_resource7 <br/> # define is_constant8 <br/> # define is_constant_array9
The Value Type zvalue_value is also defined in this file:
Typedef union _ zvalue_value {<br/> long lval;/* long value */<br/> double dval; /* double value */<br/> struct {<br/> char * val; <br/> int Len; <br/>} STR; <br/> hashtable * HT;/* hash table value */<br/> zend_object_value OBJ; <br/>} zvalue_value;
1. boolean variables
Type = is_bool, The lval field in value is the value, and the lval value is)
In {phpsrc}/Zend/zend_operators.h, a macro is defined to take a Boolean value of zval:
# Define z_bval (zval) (zend_bool) (zval). value. lval)
2. Integer Variables
Type = is_long, The lval field in value is the value
# Define z_lval (zval). value. lval
3. Floating Point Variables
Type = is_double. The dval field in value is the value.
# Define z_dval (zval). value. dval
4. string variables
Type = is_string. The STR structure in value is valid. In this structure, VaR is the string pointer and Len is the string length.
# Define z_strval (zval). value. Str. Val // obtain the string pointer
# Define z_strlen (zval). value. Str. Len // obtain the string length
The other complex types are not detailed here. For array types, the HT field takes effect. HT is a pointer to a hash table, and arrays exist in the form of hash tables. For object type variables, OBJ stores the information of this object. For resource type variables, lval is used to indirectly save this resource as an identifier.
In this way, the engine uses a zval type to implement all PHP variable types. Once the above principles are known, the type conversion of variables is easy to implement, various types of conversion macros are defined in {phpsrc}/Zend/zend_operators.c. For example, to convert a macro of the boolean type to zendi_convert_to_boolean, the main idea is to first convert the type to is_bool, then convert the values of different types of original variables to 0 or 1 of lval according to certain rules.