PHP execution principle php is a very simple application language with extremely high development efficiency. its weak type variables can save programmers a lot of time and effort to define variables and type conversion. It is a dynamic language suitable for web development. 1. Principles and features of php design multi-process model: this...
PHP execution principle
Php is a language with extremely simple application and high development efficiency. its weak type variables can save programmers a lot of time and effort to define variables and type conversion. It is a dynamic language suitable for web development.
1. Principles and features of php design
Multi-process model: in this way, processes are not affected, and process resources are used more quickly and conveniently.
Weak language: different from strong languages such as C, C ++, and java, the type of variables in php is not determined at the beginning, but is determined at runtime, it can be converted implicitly or explicitly, which makes it very flexible during development. programmers do not need to pay attention to the variable type issues.
Zend Engine + component (ext) mode reduces internal coupling
The middle layer (sapi) isolates web server and php
The syntax is simple and flexible, with few specifications. This has advantages and disadvantages...
2. four-layer php System
PHP associated array: Joined array is a typical hash_table application. A query process goes through the following steps (as can be seen from the code, this is a common hash query process and adds some quick determination to accelerate search ):
01 getKeyHashValue h;02 index = n & nTableMask;03 Bucket *p = arBucket[index];04 while (p) {05 if ((p->h == h) && (p->nKeyLength == nKeyLength)) {06 RETURN p->data; 07 }08 p=p->next;09 }10 RETURN FALTURE;
PHP index array: Index array is a common array, which is accessed by subscript. For example, arr [0] and Zend HashTable are internally performedNormalization, The index type key is also assignedHash value and nKeyLength(0 ). The inner member variable nNextFreeElement is the largest id currently allocated. it is automatically added after each push. Only in this way can PHP achieve a mixture of association and non-association.Due to the special nature of the push operation, the order of index keys in the PHP array is not determined by the subscript size, but by the push sequence.For example, arr [1] = 2; arr [2] = 3; for keys of the double type, Zend HashTable treats them as index keys.
5.2 PHP variable implementation principle
PHP is a weak type language that does not strictly distinguish the type of variables. PHP variables can be divided into simple types (int, sting, bool), set types (array, resource, object), and constants (const). all the variables are of the same structure at the underlying layer.Zval
Zval is a very important data structure in zend. it is used to mark and implement php variables. its data structure is as follows:
struct _zval_struct { zvalue_value value; /* value */ zend_uint refcount__gc; /* variable ref count */ zend_uchar type; /* active type */ zend_uchar is_ref__gc; /* if it is a ref variable */};typedef struct _zval_struct zval;
Where,
typedef union _zvalue_value { long lval; /* long value */ double dval; /* double value */ struct { /* string */ char *val; int len; } str; HashTable *ht; /* hash table value,used for array */ zend_object_value obj; /* object */} zvalue_value;
Zend_uint refcount _ gcIs a counter, used to save how many variables (or symbols, symbols) point to the zval. When a variable is generated, its refcount = 1. a typical value assignment operation, for example, $ a = $ B, will increase the refcount of zval by 1, while the unset operation will subtract 1. Before PHP5.3, the reference counting mechanism was used to implement GC. if the refcount of a zval is less than 0, the Zend Engine considers that no variable points to the zval, therefore, the memory occupied by the zval will be released. However, things are sometimes not that simple. Later we will see that the reference counting mechanism alone cannot GC the zval of circular references, even if the variable pointing to this zval has been unset, leading to Memory leakage (Memory Leak ).
Zend_uchar typeThis field is used to indicate the actual type of the variable. Variables in PHP include four scalar types (bool, int, float, string), two composite types (array, object), and two special types (resource and NULL ). Within zend, these types correspond to the following macros (code location: phpsrc/Zend/zend. h)
#define IS_NULL 0#define IS_LONG 1#define IS_DOUBLE 2#define IS_BOOL 3#define IS_ARRAY 4#define IS_OBJECT 5#define IS_STRING 6#define IS_RESOURCE 7#define IS_CONSTANT 8#define IS_CONSTANT_ARRAY 9#define IS_CALLABLE 10
Is_ref _ gcThis field is used to mark whether the variable is a referenced variable. For common variables, this value is 0, and for referenced variables, this value is 1. This variable affects zval sharing and separation.
5.2.1 integer and floating point variable
Integers and floating-point numbers are one of the basic types in PHP and are also simple variables. For integers and floating-point numbers, the corresponding values are directly stored in zvalue. The data types are long and double.
It can be seen from the zvalue structure that the integer type is different from the strong type language such as c,PHP does not distinguish int, unsigned int, long, long, and so on. for it, an integer has only one type, namely long.. From this, we can see that in PHP, the value range of an integer is determined by the number of digits of the compiler, rather than being fixed. What happens if the integer in php is out of bounds? Php automatically converts integers to floating-point numbers.
For floating-point numbers, similar to integers, it does not distinguish float from double, but only has the double type.
5.2.2 character variables
Like integers, character variables are also basic and simple variables in PHP. The zvalue structure shows that in PHP, a string is composed of a pointer to the actual data and a length junction, which is similar to a string in c ++. Because the length is represented by an actual variable, different from c, its string can be binary data (including 0), and in PHP, the string length strlen is O (1) operation
Comparison of common string concatenation methods and speed:
Assume there are four variables as follows: strA = '000000'; strB = '000000'; intA = 123; intB = 456;
Now we will compare and describe the following string concatenation methods:
1 res = strA. strB and res = "strAstrB"
In this case, zend will re-malloc a piece of memory and perform corresponding processing. the speed is average.
2 strA = strA. strB
This is the fastest speed. zend will directly relloc the current strA to avoid duplicate copies.
3 res = intA. intB
This speed is slow. because implicit format conversion is required, you should avoid it as much as possible in actual programming.
4 strA = sprintf ("% s", strA, strB );
This is the slowest way, because sprintf is not a language structure in PHP, it takes a lot of time for format recognition and processing, and the mechanism is also malloc. However, the sprintf method is the most readable and can be flexibly selected based on actual conditions.
5.2.3 array variables
PHP arrays are implemented by Zend Hash Table.
How to implement the foreach operation?Foreach of an array is done by traversing the two-way linked list in hashtable. Index arrays are much more efficient than for through foreach, saving the key-> value Search. The count operation directly calls the HashTable-> NumOfElements, O (1) operation. For a string such as '123', zend is converted to its integer form. Arr ['123'] and arr [123]
5.3 PHP variable management-reference counting and copy at write time
Reference counting is widely used in memory Collection, string operations, and other places. The reference count of Zval is implemented by the member variables is_ref and ref_count. by reference counting, multiple variables can share the same data. Avoid high consumption caused by frequent copies. InDuring the value assignment operation, zend points the variable to the same zval and ref_count ++, The ref_count-1 corresponding to the unset operation. The destroy operation is performed only when the value of ref_count is reduced to 0.If a value is referenced, zend modifies is_ref to 1.
If the PHP variable shares data through reference counting, what if it changes the value of one of the variables? When Zend tries to write a variable, if zval pointed to by the variable is shared by multiple variables, it will copy a zval with ref_count as 1 and decrease the refcount of the original zval, this process is called "zval separation ". It can be seen that zend performs the copy operation only when a write operation occurs.Copy-on-write (copy at write time)
For referenced variables, the requirements are different from those of non-referenced variables,Variables that reference the value assignment must be bundled. when you modify a variable, all bound variables are modified.
5.4 PHP local variables and global variables:
How are local and global variables implemented in PHP? PHP can view a request at any time.Two symbol tables (symbol_table and active_symbol_table)The former is used to maintain global variables. The latter is a pointer pointing to the currently active variable symbol table. when the program enters a function, zend assigns it a symbol table x and points active_symbol_table to. Global and local variables are distinguished in this way.
Get variable value: the PHP symbol table is implemented through hash_table. a unique identifier is assigned to each variable. the corresponding zval is returned from the table based on the identifier.
Use global variables in the function: in the function, we can use global variables by explicitly declaring global.Create a reference for the variable with the same name in symbol_table in active_symbol_table(The value of the referenced variable will be updated together.) if there is no variable with the same name in symbol_table, it will be created first.
Refer:
PHP execution principle
Php is a language with extremely simple application and high development efficiency. its weak type variables can save programmers a lot of time and effort to define variables and type conversion. It is a dynamic language suitable for web development.
1. Principles and features of php design
Multi-process model: in this way, processes are not affected, and process resources are used more quickly and conveniently.
Weak language: different from strong languages such as C, C ++, and java, the type of variables in php is not determined at the beginning, but is determined at runtime, it can be converted implicitly or explicitly, which makes it very flexible during development. programmers do not need to pay attention to the variable type issues.
Zend Engine + component (ext) mode reduces internal coupling
The middle layer (sapi) isolates web server and php
The syntax is simple and flexible, with few specifications. This has advantages and disadvantages...
2. four-layer php System
PHP associated array: Joined array is a typical hash_table application. A query process goes through the following steps (as can be seen from the code, this is a common hash query process and adds some quick determination to accelerate search ):
01 getKeyHashValue h;02 index = n & nTableMask;03 Bucket *p = arBucket[index];04 while (p) {05 if ((p->h == h) && (p->nKeyLength == nKeyLength)) {06 RETURN p->data; 07 }08 p=p->next;09 }10 RETURN FALTURE;
PHP index array: Index array is a common array, which is accessed by subscript. For example, arr [0] and Zend HashTable are internally performedNormalization, The index type key is also assignedHash value and nKeyLength(0 ). The inner member variable nNextFreeElement is the largest id currently allocated. it is automatically added after each push. Only in this way can PHP achieve a mixture of association and non-association.Due to the special nature of the push operation, the order of index keys in the PHP array is not determined by the subscript size, but by the push sequence.For example, arr [1] = 2; arr [2] = 3; for keys of the double type, Zend HashTable treats them as index keys.
5.2 PHP variable implementation principle
PHP is a weak type language that does not strictly distinguish the type of variables. PHP variables can be divided into simple types (int, sting, bool), set types (array, resource, object), and constants (const). all the variables are of the same structure at the underlying layer.Zval
Zval is a very important data structure in zend. it is used to mark and implement php variables. its data structure is as follows:
struct _zval_struct { zvalue_value value; /* value */ zend_uint refcount__gc; /* variable ref count */ zend_uchar type; /* active type */ zend_uchar is_ref__gc; /* if it is a ref variable */};typedef struct _zval_struct zval;
Where,
typedef union _zvalue_value { long lval; /* long value */ double dval; /* double value */ struct { /* string */ char *val; int len; } str; HashTable *ht; /* hash table value,used for array */ zend_object_value obj; /* object */} zvalue_value;
Zend_uint refcount _ gcIs a counter, used to save how many variables (or symbols, symbols) point to the zval. When a variable is generated, its refcount = 1. a typical value assignment operation, for example, $ a = $ B, will increase the refcount of zval by 1, while the unset operation will subtract 1. Before PHP5.3, the reference counting mechanism was used to implement GC. if the refcount of a zval is less than 0, the Zend Engine considers that no variable points to the zval, therefore, the memory occupied by the zval will be released. However, things are sometimes not that simple. Later we will see that the reference counting mechanism alone cannot GC the zval of circular references, even if the variable pointing to this zval has been unset, leading to Memory leakage (Memory Leak ).
Zend_uchar typeThis field is used to indicate the actual type of the variable. Variables in PHP include four scalar types (bool, int, float, string), two composite types (array, object), and two special types (resource and NULL ). Within zend, these types correspond to the following macros (code location: phpsrc/Zend/zend. h)
#define IS_NULL 0#define IS_LONG 1#define IS_DOUBLE 2#define IS_BOOL 3#define IS_ARRAY 4#define IS_OBJECT 5#define IS_STRING 6#define IS_RESOURCE 7#define IS_CONSTANT 8#define IS_CONSTANT_ARRAY 9#define IS_CALLABLE 10
Is_ref _ gcThis field is used to mark whether the variable is a referenced variable. For common variables, this value is 0, and for referenced variables, this value is 1. This variable affects zval sharing and separation.
5.2.1 integer and floating point variable
Integers and floating-point numbers are one of the basic types in PHP and are also simple variables. For integers and floating-point numbers, the corresponding values are directly stored in zvalue. The data types are long and double.
It can be seen from the zvalue structure that the integer type is different from the strong type language such as c,PHP does not distinguish int, unsigned int, long, long, and so on. for it, an integer has only one type, namely long.. From this, we can see that in PHP, the value range of an integer is determined by the number of digits of the compiler, rather than being fixed. What happens if the integer in php is out of bounds? Php automatically converts integers to floating-point numbers.
For floating-point numbers, similar to integers, it does not distinguish float from double, but only has the double type.
5.2.2 character variables
Like integers, character variables are also basic and simple variables in PHP. The zvalue structure shows that in PHP, a string is composed of a pointer to the actual data and a length junction, which is similar to a string in c ++. Because the length is represented by an actual variable, different from c, its string can be binary data (including 0), and in PHP, the string length strlen is O (1) operation
Comparison of common string concatenation methods and speed:
Assume there are four variables as follows: strA = '000000'; strB = '000000'; intA = 123; intB = 456;
Now we will compare and describe the following string concatenation methods:
1 res = strA. strB and res = "strAstrB"
In this case, zend will re-malloc a piece of memory and perform corresponding processing. the speed is average.
2 strA = strA. strB
This is the fastest speed. zend will directly relloc the current strA to avoid duplicate copies.
3 res = intA. intB
This speed is slow. because implicit format conversion is required, you should avoid it as much as possible in actual programming.
4 strA = sprintf ("% s", strA, strB );
This is the slowest way, because sprintf is not a language structure in PHP, it takes a lot of time for format recognition and processing, and the mechanism is also malloc. However, the sprintf method is the most readable and can be flexibly selected based on actual conditions.
5.2.3 array variables
PHP arrays are implemented by Zend Hash Table.
How to implement the foreach operation?Foreach of an array is done by traversing the two-way linked list in hashtable. Index arrays are much more efficient than for through foreach, saving the key-> value Search. The count operation directly calls the HashTable-> NumOfElements, O (1) operation. For a string such as '123', zend is converted to its integer form. Arr ['123'] and arr [123]
5.3 PHP variable management-reference counting and copy at write time
Reference counting is widely used in memory Collection, string operations, and other places. The reference count of Zval is implemented by the member variables is_ref and ref_count. by reference counting, multiple variables can share the same data. Avoid high consumption caused by frequent copies. InDuring the value assignment operation, zend points the variable to the same zval and ref_count ++, The ref_count-1 corresponding to the unset operation. The destroy operation is performed only when the value of ref_count is reduced to 0.If a value is referenced, zend modifies is_ref to 1.
If the PHP variable shares data through reference counting, what if it changes the value of one of the variables? When Zend tries to write a variable, if zval pointed to by the variable is shared by multiple variables, it will copy a zval with ref_count as 1 and decrease the refcount of the original zval, this process is called "zval separation ". It can be seen that zend performs the copy operation only when a write operation occurs.Copy-on-write (copy at write time)
For referenced variables, the requirements are different from those of non-referenced variables,Variables that reference the value assignment must be bundled. when you modify a variable, all bound variables are modified.
5.4 PHP local variables and global variables:
How are local and global variables implemented in PHP? PHP can view a request at any time.Two symbol tables (symbol_table and active_symbol_table)The former is used to maintain global variables. The latter is a pointer pointing to the currently active variable symbol table. when the program enters a function, zend assigns it a symbol table x and points active_symbol_table to. Global and local variables are distinguished in this way.
Get variable value: the PHP symbol table is implemented through hash_table. a unique identifier is assigned to each variable. the corresponding zval is returned from the table based on the identifier.
Use global variables in the function: in the function, we can use global variables by explicitly declaring global.Create a reference for the variable with the same name in symbol_table in active_symbol_table(The value of the referenced variable will be updated together.) if there is no variable with the same name in symbol_table, it will be created first.
For more articles on PHP execution principles, refer to the Chinese PHP website!