Maybe you know, maybe you don't know, PHP is a weak type, dynamic scripting language. The so-called weak type means that PHP does not strictly verify the variable type (strictly speaking, PHP is a medium-strong language, which will be described in future articles ), when declaring a variable, you do not need to specify the type of data it stores:
- <?php
- $var = 1; //int
- $var = "laruence"; //string
- $var = 1.0002; //float
- $var = array(); // array
- $var = new Exception('error'); //object;
Dynamic language, that is, the PHP language structure can be changed at runtime. For example, we require a function definition file at runtime, which leads to dynamic changes to the function table of the language.
The so-called scripting language means that PHP is not run independently. To run php, we need a PHP parser:
- /usr/bin/php -f example.ph
As I have mentioned in the previous article, PHP is executed through Zend engine (Ze, Zend engine), And Ze is written in C. Everyone knows that C is a strongly typed language, that is, all variables in C can only save one type of data when declared to the final destruction. So how does PHP implement weak types based on ze?
In PHP, all variables are saved using a structure-zval. In Zend/Zend. H, we can see the definition of zval:
- typedef struct _zval_struct {
- zvalue_value value;
- zend_uint refcount;
- zend_uchar type;
- zend_uchar is_ref;
- } zval;
-
Zvalue_value is the key part of data storage. Now it's time to reveal the answer. How does PHP implement weak types based on ze? Because zvalue_value is a union ),
- 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 various types of PHP?
Common variable types in PHP Include:
- 1. Integer/floating point/long integer/bool value, etc.
- 2. String
- 3. array/associated array
- 4. Object
- 5. Resources
-
PHP stores the real type of a Variable Based on the Type field in zval, and then selects how to obtain the zvalue_value value based on the type, such as for integer and bool values:
- Zval. type = IS_LONG; // integer
- Zval. type = IS_BOOL; // Boolean
Take zval. value. lval. For the bool value, lval ε (0 | 1 );
For dual precision or float, the zval. Value dval is obtained.
If it is a string, then:
- zval.type = IS_STRIN
At this time, it will take:
Zval. value. Str
This is also a structure that contains the length of the C-shard string and the string.
For arrays and objects, the type corresponds to IS_ARRAY and IS_OBJECT, and the corresponding values are zval. value. ht and obj respectively.
Resources are special. in PHP, resources are special variables. Any variables that do not belong to the built-in variable type in PHP will be considered as resource sources for storage. For example, database handle, open file handle, and so on. For resources:
- type = IS_RESOURC
At this time, zval will be retrieved. value. lval. At this time, lval is an integer indicator. Then, PHP will query the corresponding resources in the PHP built-in resource list based on this indicator (this part of content, I will introduce it in a separate article later). Currently, you only need to know the lval at this time as it corresponds to the offset value of the resource linked list.
- ZEND_FETCH_RESOURCE(con, type, zval *, default, resource_name, resource_type
);
Using this mechanism, PHP implements a weak type, because for ZE, it will always be faced with the same type, that is, zval.
Ps: The team will go out to building tomorrow. I thought I should write something for my blog reader before leaving to spend the weekend. Today, let's start with something simple. Next time, I will introduce the PHP variables, scopes, and the copy on write and change on write mechanisms of variables ....