PHP kernel exploration: variable storage and type usage Instructions _php tips

Source: Internet
Author: User
Tags garbage collection php language zend

Answer the question in the previous section first.

Copy Code code as follows:

$foo = 10;
$bar = 20;

function Change () {
Global $foo;
echo ' function internal $foo = '. $foo. ' <br/> ';
If the $bar is not defined as a global variable, the function body cannot access the $bar
$bar = 0;
$foo + +;

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

Program Output 11 20. The reason is that the $bar variable cannot be accessed within the method, so its value is still 20. After using global, you can get the $foo value, and the $foo value is 11.
Global is defined globally, but this global variable is not applied to the entire Web site, but is applied to the current page, including all files with include or require.
The preface mentions three basic characteristics of a variable, one of which is the type of the variable, and the variable has a specific type, such as a string, array, object, and so on. The type system of a programming language can be divided into two types: strong type and weak type.
A strongly typed language is when a variable is declared as a variable of a type, it is not possible to assign a value other than the type of the variable to it when the program is running (not quite, of course), which may involve the conversion of the type, which is described later in the section, and C/c++/java and other languages are part of this category.
Scripting languages such as PHP and Ruby,javascript are weakly typed languages: A variable can represent any type of data.
A large part of the reason why PHP becomes a simple and powerful language is that it has weakly typed variables. But sometimes this is a double-edged sword, improper use will also bring some problems. As with instruments, the more powerful they are, the greater the likelihood of errors.
Inside the official PHP implementation, all variables are saved using the same data structure (ZVAL), which also represents various data types in PHP. It contains not only the value of the variable, but also the type of the variable. This is the core of the weak type of PHP.
How does the ZVAL structure specifically implement the weak type, and we'll come together to uncover the veil.
Variable storage structure
When you declare or use a variable, PHP does not need to explicitly indicate its data type.
PHP is a weak type language, which does not mean that PHP does not have a type, in PHP, there are 8 types of variables, can be divided into three categories
* Scalar type: boolean, Integer, float (double), string
* Compound Type: Array, object
* Special Type: resource, NULL
The official PHP is implemented in C, and C is a strongly typed language, so how do you implement the weak type in PHP?
The value of the variable is stored in the ZVAL structure shown below. The ZVAL structure is defined in the Zend/zend.h file, and its structure is as follows:

Copy Code code 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 for a variable. Unlike other compiled static languages, PHP stores variable types of PHP user space in the same structure when saving variables. So that we can get the type of the variable through this information.
There are four fields in the zval structure, the meanings of which are:

Property name Meaning Default value
refcount__gc Represents a reference count 1
is_ref__gc Indicates whether it is a reference 0
Value Store the value of a variable
Type Variable-specific type

After PHP5.3, a new garbage collection mechanism was introduced, with reference counts and referenced field names changed to REFCOUNT__GC and IS_REF__GC. Before this is refcount and is__ref.

The value of the variable is stored in the zvalue_value of the other struct body. The value is stored as described below.
PHP user space refers to the level of PHP language, and most of the book discusses the implementation of PHP. These implementations can be understood as kernel space. Because PHP is implemented using C, the scope of this space is limited to the C language. PHP user space will be limited by the PHP syntax and functionality provided by the scope. For example, some PHP extensions provide some PHP functions or classes, which is to export the method or class to the PHP user space.
Variable type
The Type field of the ZVAL structure is the most critical field for implementing a weak type, and the value of type can be: Is_null, Is_bool, Is_long, is_double, is_string, Is_array, Is_object, and Is_ One of the RESOURCE. Literally, they're just a unique indicator of types, and different values are stored in the Value field depending on the type. In addition, there are is_constant and is_constant_array with the types they define.
This is similar to the way we designed the database, to avoid repeating the design of similar tables, using a single labeled field to record different types of data.

The value of the variable is stored
The value of the variables mentioned earlier is stored in the Zvalue_value consortium, and the structure is defined as follows:

Copy Code code 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;

The use of a federation instead of a struct is for spatial utilization, because a variable can only belong to one type at a time. If you use the structure, you will waste space unnecessarily, and all the logic in PHP revolves around the variable, so that the memory waste will be very large. The cost is small but the benefits are very high.
Various types of data use different methods for storing variable values, and the corresponding assignments are as follows:

1. General type

Variable type Macro ?
Boolean Zval_bool The variable value of a Boolean/integral type is stored in (zval). Value.lval, and its type is also stored in the appropriate is_*.
Z_type_p (z) =is_bool/long; Z_lval_p (Z) = ((b)!=0);
Integer Zval_long
Float Zval_double
Null Zval_null The value of a NULL value does not need to be stored, just the (zval). Type is labeled Is_null.
Z_type_p (z) =is_null;
Resource Zval_resource The storage of resource types is the same as other general variables, but its initialization and access implementations are different.
Z_type_p (z) = Is_resource; Z_lval_p (z) = l;

2. String sting
The type of string is the same as other data types, but there is one more field of string length when storing the string.

Copy Code code as follows:

struct {
Char *val;
int Len;
} str;

The string in C is an array of characters that end with a number of strings, which stores more of the length of the string, which is the same as the redundant fields we added when designing the database. Because the time complexity of getting the length of the string in real time is O (n), and the operation of the string is very frequent in PHP, it avoids the repetition of the length of the string, which can save a lot of time and is a way of changing time in space. So look in PHP the strlen () function can get the length of the string in constant time. Strings are very much manipulated in computer languages, so the length of strings is stored in most high-level languages.

3. Arrays Array

Arrays are the most common and powerful variable types in PHP, which can store other types of data and provide a variety of built-in action functions. The storage of an array is more complex than other variables, and the value of the array is stored in the field, which is a hashtable type of data. The PHP array uses a hash table to store the associated data. A hash table is an efficient key-value pair storage structure. PHP's hash table implementation uses two data structures hashtable and bucket. All of PHP's work is implemented by a hash table, in the next section Hashtable the basic concept of the hash table and the hash table implementation of PHP.

4. Objects Object

In object-oriented languages, we can define the data types we need, including the properties of classes, methods, and so on. The object is a concrete implementation of the class. The object has its own state and the actions it can complete.
The object of PHP is a composite type of data that is stored using a zend_object_value structure. The definition is as follows:

Copy Code code as follows:

typedef struct _ZEND_OBJECT_VALUE {
Zend_object_handle handle; unsigned int type, EG (objects_store). Index of Object_buckets
Zend_object_handlers *handlers;
} Zend_object_value;

The object in PHP is created only at run time, and the previous section describes the eg macro, which is a global structure that is used to hold data at run time. This includes the pool of objects used to hold all the objects created, EG (Objects_store), and the Zend_object_handle field of the object value content is the index of the current object in the object pool. The Handlers field is the process function that is stored when an object is manipulated. The structure of the structure and object-related classes is _zend_class_entry, which is described later.
PHP's weak-variable container is implemented in a form that is all-inclusive and has a corresponding tag and storage space for each type of variable. Using a strongly typed language is generally more efficient than a weak type, because a lot of information can be determined before it is run, which can also help you troubleshoot bugs. The problem with this is that writing code is relatively constrained.

The main purpose of

PHP is as a web development language, and bottlenecks in ordinary Web applications are usually at the level of business and data access. But language can also be a key factor in large applications. Facebook therefore uses its own PHP implementation. Compile PHP as C + + code to improve performance. But Facebook's hiphop is not a complete PHP implementation, because it is directly compiled PHP into C + +, there are some PHP dynamic features such as the eval structure can not be achieved. Of course, there is a way to achieve, hiphop not implementation should also make a trade-off.

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