Home > Backend Development > PHP Tutorial > In-depth understanding of PHP principles (Variables inside PHP)

In-depth understanding of PHP principles (Variables inside PHP)

angryTom
Release: 2023-04-07 16:24:01
forward
2000 people have browsed it

In-depth understanding of PHP principles (Variables inside PHP)

Maybe you know, maybe you don’t know, PHP is a weakly typed, 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 type language, this part will be described in a future article), when declaring a variable , there is no need to explicitly indicate the type of data it saves:

<?php
  $var = 1; //int
  $var = "laruence"; //string
  $var = 1.0002; //float
  $var = array(); // array
  $var = new Exception(&#39;error&#39;); //object;
Copy after login

Dynamic language, that is to say, the language structure of PHP can be changed during runtime, for example, we require during runtime A function definition file, which causes dynamic changes to the language's function table.

The so-called scripting language means that PHP does not run independently. To run PHP we need a PHP parser:

/usr/bin/php -f example.php
Copy after login

I have already mentioned in the previous article that the execution of PHP is through Zend engine (ZE, Zend engine), ZE is written in C. Everyone knows that C is a strongly typed language, that is to say, all variables in C can only save one type from the time they are declared to the final destruction. type of data. 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;
Copy after login

where zvalue_value is true Now it’s time to reveal the key to saving data. 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;
Copy after login

So how does this structure store multiple types in PHP?

Common variable types in PHP are:

  • 1. Integer/floating point/long integer/bool value, etc.

  • 2. String

  • 3. Array/associative array

  • 4. Object

  • 5. Resources

PHP stores the real type of a variable based on the type field in zval, and then chooses how to obtain the value of zvalue_value based on type. For example, for integers and bool values:

   zval.type = IS_LONG;//整形
   zval.type = IS_BOOL;//布尔值
Copy after login

Get zval.value.lval, for bool values ​​lval∈(0|1);

If it is double precision, or float Then it will get the dval of zval.value.

And if it is a string, then:

   zval.type = IS_STRING
Copy after login

At this time, it will be: zval.value.str

And this is also a structure , storing the string in C format and the length of the string.

For arrays and objects, type corresponds to IS_ARRAY, IS_OBJECT respectively, and the corresponding ones are zval.value.ht and obj

## respectively. #What is special is the resource. In PHP, the resource is a very special variable. Any variable that does not belong to the built-in variable type of PHP will be regarded as a resource for saving, such as database handle, open file handle, etc. . For resources:

   type = IS_RESOURCE
Copy after login

At this time,

zval.value.lval will be fetched. At this time, lval is an integer indicator, and then PHP will use this indicator in PHP Query the corresponding resources in a resource list you created (I will introduce this part in a separate article in the future). For now, you only need to know that the lval at this time seems to be the offset value corresponding to the resource list.

 ZEND_FETCH_RESOURCE(con, type, zval *, default, resource_name, resource_type);
Copy after login
By borrowing this mechanism, PHP implements weak types, because for ZE, it always faces the same type, that is zval.

For more PHP related knowledge, please visit

PHP Chinese website!

The above is the detailed content of In-depth understanding of PHP principles (Variables inside PHP). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:www.laruence.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template