Analysis on the variable of PHP principles (VariablesinsidePHP)

Source: Internet
Author: User
As mentioned in the previous article, PHP is executed through Zendengine (ZE, Zend engine), And ZE is written in C. We all know that C is a strongly typed language, that is, all variables in C are declared to the most

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 are declared to the most

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:

The Code is as follows:


$ 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:

The Code is as follows:


/Usr/bin/php-f example. ph


As mentioned in the previous article, PHP is executed through Zend engine (ZE, Zend engine), ZE is written in C, and website space. As we all know, C is a strong language, that is to say, all the variables in C can only save one type of data when they are declared and finally destroyed. So how does PHP implement weak types based on ZE?

In PHP, all the variables in the VM are saved using a structure-zval. In Zend/zend. h, we can see the definition of zval:

The Code is as follows:


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 ),

The Code is 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 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:

The Code is as follows:


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:

The Code is as follows:


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:

The Code is as follows:


Type = IS_RESOURC


At this time, the virtual host will fetch zval. 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.

The Code is as follows:


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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.