Php variable principle, php variable
1. As a weak type language, php does not need to explicitly specify the type of the variable, but php variables are also of the type. php variables include the following eight variables (three categories)
A. scalar type: boolean, integer, float (double), string
B. Composite Type: array, object
C. Special Type: resource, null
2. How php uses C language to implement Variables
A. Variable Storage Structure
Typedef struct _ zval_struct zval ;... struct _ zval_struct {/* Variable information */zvalue_value value;/* store the Variable value, which is a union type */zend_uint refcount _ gc;/* Reference count of the Variable, the default value is 1 */zend_uchar type;/* variable type, which is one of IS_NULL, IS_BOOL, IS_LONG, IS_DOUBLE, IS_STRING, IS_ARRAY, IS_OBJECT, and IS_RESOURCE */zend_uchar is_ref _ gc; /* indicates whether it is a reference */};
B. The stored variable is worth zvalue_value as follows:
typedef union _zvalue_value { long lval; /* long value */ double dval; /* double value */ struct { char *val; int len; } str; HashTable *ht; /* hash table value */ zend_object_value obj;} zvalue_value;
Here union is used instead of struct to save memory space, so that a variable can only represent one type at a time.
Reference: tipi open source project http://www.php-internals.com/book? P = chapt03/03-01-00-variables-structure