PHP kernel exploration: usage instructions on variable storage and types

Source: Internet
Author: User
This article describes how to explore the PHP kernel: Variable storage and type related information. For more information, see

This article describes how to explore the PHP kernel: Variable storage and type related information. For more information, see

Answer the question in the previous section first.

The Code is as follows:


$ Foo = 10;
$ Bar = 20;

Function change (){
Global $ foo;
// Echo 'function $ foo = '. $ foo .'
';
// If $ bar is not defined as a global variable, $ bar cannot be accessed in the function body.
$ Bar = 0;
$ Foo ++;
}

Change ();
Echo $ foo, '', $ bar;
?>

Program output 11 20. The reason is that the $ bar variable cannot be accessed inside the method, so its value is still 20. After using global, you can get the value of $ foo. The value of $ foo after auto-increment is 11.
Global is used to define Global variables. However, this Global variable is not applied to the entire website, but to the current page, including all files including include or require.
The preface mentions three basic features of a variable. One of these features is the type of a variable. All variables have specific types, such as strings, arrays, and objects. Programming Language type systems can be divided into two types: strong type and weak type:
A strongly typed language is a variable that is declared as a certain type. During the program running, a value other than the type of the variable cannot be assigned to it (of course, this is not the case, this may involve conversions of types, which will be introduced in the following sections). C/C ++/Java and other languages belong to this type.
Scripting languages such as PHP, Ruby, and JavaScript are weak types: a variable can represent any data type.
PHP becomes a simple and powerful language because it has weak variables. However, sometimes this is a double-edged sword. improper use may also cause some problems. Like an instrument, the more powerful the function is, the more likely an error will occur.
In the official PHP implementation, all variables are saved using the same data structure (zval), which simultaneously represents various data types in PHP. It not only contains the value of the variable, but also the type of the variable. This is the core of the weak PHP type.
Then how does the zval structure implement the weak type? Let's unveil it together.
Variable Storage Structure
PHP does not need to explicitly specify the data type when declaring or using variables.
PHP is a weak type language, which does not indicate that PHP has no type. In PHP, there are eight variable types, which can be divided into three types.
* Scalar type: boolean, integer, float (double), string
* Compound types: array and object
* Special types: resource and NULL
Official PHP is implemented in C, while C is a strong language. How can this be achieved in PHP?
The value of the variable is stored in the zval struct shown below. The zval struct is defined in Zend/zend. h file. Its structure is as follows:

The Code is as follows:


Typedef struct _ zval_struct zval;
...
Struct _ zval_struct {
/* Variable information */
Zvalue_value value;/* value */
Zend_uint refcount _ gc;
Zend_uchar type;/* active type */
Zend_uchar is_ref _ gc;
};

PHP uses this structure to store all the data of the variable. Unlike other compilation static languages, PHP stores the variable types in the PHP user space in the same struct when storing variables. In this way, we can get the type of the variable through the information.
The zval struct contains four fields, which are described as follows:

Default attribute Name Description

Refcount _ gc indicates reference count 1

Is_ref _ gc indicates whether to reference 0

Value stores the value of a variable.

Specific type of the type Variable


After PHP5.3, A New garbage collection mechanism was introduced. The reference count and referenced field names were changed to refcount _ gc and is_ref _ gc. Before that, it is refcount and is _ ref.

The variable value is stored in the zvalue_value structure. For more information about value storage, see the following section.
PHP user space refers to the PHP language, and the implementation of PHP is mostly discussed in this book. These implementations can be understood as kernel space. Since PHP is implemented using C, the scope of this space is limited to C language. PHP user space is limited by the scope provided by PHP syntax and functions. For example, some PHP extensions provide some PHP functions or classes, which are exported to the PHP user space.
Variable type
The type field of the zval struct is the most critical field to implement weak types. The value of type can be IS_NULL, IS_BOOL, IS_LONG, IS_DOUBLE, IS_STRING, IS_ARRAY, IS_OBJECT, and IS_RESOURCE. Literally, they are only unique identifiers of types. Different values are stored in the value Field according to different types. In addition, the types defined with them are IS_CONSTANT and IS_CONSTANT_ARRAY.
This is similar to our database design practice. To avoid repeated design of similar tables, we use a field to record different types of data.

Variable value Storage
The variable values are stored in the zvalue_value consortium. The struct is defined as follows:

The Code is as follows:


Typedef union _ zvalue_value {
Long lval;/* long value */
Double dval;/* double value */
Struct {
Char * val;
Int len;
} Str;
HashTable * ht;/* hash table value */
Zend_object_value obj;
} Zvalue_value;

Here we use a consortium instead of a struct for space utilization, because a variable can belong to only one type at the same time. If struct is used, it will be unnecessary to waste space, and all the logic in PHP is centered around variables. In this way, the memory waste will be very large. This approach is costly but highly profitable.
Different types of data store variable values using different methods. The corresponding value assignment method is as follows:

1. General Type

Variable type macro?

BooleanZVAL_BOOL Boolean/Integer variable values are stored in (zval). value. lval, and their types are stored as corresponding IS.
Z_TYPE_P (z) = IS_BOOL/LONG; Z_LVAL_P (z) = (B )! = 0 );

IntegerZVAL_LONG

FloatZVAL_DOUBLE

The variable value of nullZVAL_NULLNULL does not need to be stored. You only need to mark (zval). type as IS_NULL.
Z_TYPE_P (z) = IS_NULL;

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.