Analysis of PHP principle variables (Variables inside PHP) _php tutorial

Source: Internet
Author: User
Maybe you know, maybe you don't know that PHP is a weak type, 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 a later article), when declaring a variable, do not need to display the type of data that indicates it to save:
Copy CodeThe 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 language structure of PHP can be changed at runtime, for example, we require a function definition file at run time, which causes 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 the code code as follows:
/usr/bin/php-f example.ph

As I have already said in the previous article, PHP is executed through Zend engine (ZE, Zend Engine), ZE is written in C, As we all know, C is a strongly typed language, that is, all variables in C can only hold one type of data when it is declared to final destruction. So how does PHP implement the weak type on the basis of ze?

In PHP, all variables are stored in a structure-zval, and in zend/zend.h we can see the definition of Zval:
Copy the 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 a key part of really preserving data, now it's time to reveal the answer, how does PHP implement the weak type on ze basis? Because Zvalue_value is a consortium (union),
Copy the 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 multiple types of PHP?

The types of variables that are common in PHP are:
1. Integer/floating-point/Long integer/bool value, etc.
2. String
3. Array/associative array
4. Objects
5. Resources

PHP stores the true type of a variable based on the Type field in Zval, and then chooses how to get the value of the Zvalue_value based on the type, for example, Integer and bool values:
Copy CodeThe code is as follows:
Zval.type = is_long;//Shaping
Zval.type = is_bool;//Boolean

Go to fetch zval.value.lval, for bool value lval∈ (0|1);
If it is double precision, or float will fetch Zval.value's dval.
And if it's a string, then:
Copy CodeThe code is as follows:
Zval.type = Is_strin

This time, you will take:
Zval.value.str
And this is also a structure that has a C-cell string and the length of the string.
For arrays and objects, the type corresponds to Is_array, Is_object, respectively, zval.value.ht and obj.
In particular, resources, in PHP, resources are a very special variable, any variables that are not part of PHP's built-in variable types are considered as resources to save, such as database handles, open file handles, and so on. For resources:
Copy CodeThe code is as follows:
Type = IS_RESOURC

This time, will go to fetch zval.value.lval, at this time the lval is an integer indicator, and then PHP will be based on this indicator in PHP built in a resource list to query the corresponding resources (this part of the content, I will open a separate article to introduce), at present, All you need to know is that the lval at this point is like an offset value corresponding to the resource list.
Copy CodeThe code is as follows:
Zend_fetch_resource (Con, type, Zval *, default, resource_name, Resource_type);

With this mechanism, PHP implements a weak type, because for ze, it always faces the same type, that is zval.

http://www.bkjia.com/PHPjc/328118.html www.bkjia.com true http://www.bkjia.com/PHPjc/328118.html techarticle Maybe you know, maybe you don't know that PHP is a weak type, 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 class ...)

  • 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.