A deep understanding of the Variables in PHP principles (Variables inside PHP)

Source: Internet
Author: User

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:

 
 
  1. <?php
  2.   $var = 1; //int
  3.   $var = "laruence"; //string
  4.   $var = 1.0002; //float
  5.   $var = array(); // array
  6.   $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:

 
 
  1.   /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:

 
 
  1.   typedef struct _zval_struct {
  2.     zvalue_value value;
  3.     zend_uint refcount;
  4.     zend_uchar type;
  5.     zend_uchar is_ref;
  6.   } zval;
  7.  

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

 
 
  1. typedef union _zvalue_value {
  2.     long lval;
  3.     double dval;
  4.     struct {
  5.         char *val;
  6.         int len;
  7.     } str;
  8.     HashTable *ht;
  9.     zend_object_value obj;
  10. } zvalue_value
    1. ;

So how does this structure store various types of PHP?
Common variable types in PHP Include:

 
 
  1. 1. Integer/floating point/long integer/bool value, etc.
  2. 2. String
  3. 3. array/associated array
  4. 4. Object
  5. 5. Resources
  6.  

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:

 
 
  1. Zval. type = IS_LONG; // integer
  2. 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:

 
 
  1.    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:

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

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

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.