Maybe you know, maybe you don't know, PHP is a weakly typed, dynamic scripting language. The so-called weak type, that is, PHP does not strictly verify the variable type (strictly speaking, PHP is a medium-strong type language, this part will be described in future articles), when declaring a variable, do not need to show the type of data it holds:
Copy Code code as follows:
<?php
$var = 1; Int
$var = "Laruence"; String
$var = 1.0002; Float
$var = Array (); Array
$var = new Exception (' Error '); Object
Dynamic language, that is, the language structure of PHP can be changed during the runtime, such as we require a function definition file at runtime, which results in the dynamic change of the function table of the language.
The so-called scripting language, that is, PHP is not run independently, to run PHP we need the PHP parser:
Copy Code code as follows:
/usr/bin/php-f example.ph
As I have said in previous articles, PHP is executed through the Zend engine (ze, Zend engine), Ze is written in C, and everyone knows that C is a strongly typed language, that is, all variables in C can only hold one type of data when it is declared to be destroyed. So how does PHP implement a weak type based on ze?
In PHP, all variables are saved with a struct-zval, and in zend/zend.h we can see the definition of Zval:
Copy Code code as follows:
typedef struct _ZVAL_STRUCT {
Zvalue_value value;
Zend_uint RefCount;
Zend_uchar type;
Zend_uchar Is_ref;
} Zval;
Where Zvalue_value is the key part of really saving data, now it's time to reveal the answer, how does PHP implement the weak type on the basis of ze? Because Zvalue_value is a union,
Copy Code code as follows:
typedef Union _ZVALUE_VALUE {
Long lval;
Double Dval;
struct {
Char *val;
int Len;
} str;
HashTable *ht;
Zend_object_value obj;
} Zvalue_value;
So how does this structure store a variety of types in PHP?
The types of variables that are common in PHP are:
1. Integer/float/Long integer/bool value, etc.
2. String
3. Array/associative array
4. The object
5. Resources
PHP stores the true type of a variable based on the Type field in Zval, and then chooses how to get the Zvalue_value value based on type, such as Integer and bool values:
Copy Code code as follows:
Zval.type = is_long;//Shaping
Zval.type = is_bool;//Boolean
To take zval.value.lval, for the bool value lval∈ (0|1);
If it is double precision, or float will go to fetch Zval.value's dval.
And if it's a string, then:
Copy Code code as follows:
This time, it will take:
Zval.value.str
This is also a structure that contains the C-grid string and the length of the string.
For arrays and objects, the type corresponds to Is_array, Is_object, and zval.value.ht and obj, respectively.
The more special is the resource, in PHP, the resource is a very special variable, any variable that does not belong to PHP built-in variables, will be considered as a resource to save, for example, database handle, open file handle and so on. For resources:
Copy Code code as follows:
This time, will go to fetch zval.value.lval, at this time the lval is an integer indicator, then PHP will be based on this indicator in PHP built a resource list to query the corresponding resources (this part of the content, I will open a separate article to introduce), at present, You just know that the lval at this point is like the offset value of the resource list.
Copy Code code as follows:
Zend_fetch_resource (Con, type, Zval *, default, resource_name, Resource_type);
Using such a mechanism, PHP has a weak type, because for ze, it is always facing the same type, that is zval.