Document directory
- So, question 1: How does the Zend engine use C to implement this weak type?
- Question 2: How does the Zend engine identify and store various data types in PHP?
PHP is a weak type, dynamic language script. When declaring a variable, you do not need to specify the data type it stores.
For example:
<?php$var = 1;$var = "variable";$var = 1.00;$var = array();$var = new Object();
Dynamic variables can be changed during running, and do not need to declare the variable type before use.
So, question 1: How does the Zend engine use C to implement this weak type?
In fact, the variables declared in PHP are saved using the zval struct in ze.
First, let's open Zend/Zend. h to see the definition of zval:
typedef struct _zval_struct zval;struct _zval_struct {/* Variable information */zvalue_value value;/* value */zend_uint refcount__gc;zend_uchar type;/* active type */zend_uchar is_ref__gc;};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;
Zend/zend_types.h:
typedef unsigned char zend_bool;typedef unsigned char zend_uchar;typedef unsigned int zend_uint;typedef unsigned long zend_ulong;typedef unsigned short zend_ushort;
From the code above, we can see that _ zvalue_value is the key part of truly saving data. Declaration of weak type variables implemented through the shared body
Question 2: How does the Zend engine identify and store various data types in PHP? _ Zval_struct.type stores the real type of a variable and selects how to obtain the value of zvalue_value Based on the type.
Type Value List (Zend/Zend. h): # define attributes # define is_long1 # define is_double2 # define attributes # define is_array4 # define is_object5 # define is_string6 # define is_resource7 # define is_constant8 # define attributes
Let's look at a simple example:
<? PHP $ A = 1; // zval. type = is_long, then zval. value to obtain lval. $ A = array (); // zval. type = is_array, then zval. value to get ht.
This is the most complex one, and "Resource Type" is often used in third-party extension development ".
In PHP, any variable that does not belong to the built-in variable type of PHP will be considered as a resource for saving.
For example, database handle, opened file handle, and opened socket handle.
Resource type, which must be registered using the API functions provided by Ze. The declaration and use of resource variables are described in a separate article.
It is precisely because of the processing method like ze that PHP implements a weak type, and for ze, it will always face the same type of zval.