Home>Article>Backend Development> [PHP Learning] Data Types of PHP7

[PHP Learning] Data Types of PHP7

little bottle
little bottle forward
2019-04-18 16:29:47 1900browse

Variable name in PHP → zval, variable value → zend_value. Its variable memory is managed through reference counting. In PHP7, the reference counting is in the value structure.

Variable type:

Header file is implemented internally in PHP source code/zend/zend_types.h

##:

PHP represents a variable through the structure zval, and variable values of different types are represented through a union embedded in zval, that is, zend_value.

## zend_value is a union, its code is as follows:

ast Types such as , ptr, and zv are only used by the kernel itself.

String:

PHP defines a separate structure for strings: zend_string. In zend_value, str points to the specific structure.

The val that stores the string content is special.

val does not use the char* type. When allocating a string, it is operated like this: malloc(sizeof(zend_sting) string length), which means that more memory will be allocated to store the string content. This block The starting position of the extra memory is val.

The advantage of this is that it can save one memory allocation (char*) and is more helpful for memory management.

The extra byte in val (val[1] instead of val[0] in the structure) is used to store the last character "\0" of the stored string.

For example $a="abc", the corresponding zend_string memory structure is as shown on the left:

Array:

##nTableMask:

This value is used in the hash function according to the hash code of the key Used when the snake element is stored as a position. nTableMask = -nTableSize or nTableMask = ~nTableSize 1.

nNumUsed, nNumOfElements:

When deleting an array element, it will not be deleted from the array immediately. Instead, the type of the element will be marked as IS_UNDEF. It will only be used when the array capacity exceeds the limit and needs to be expanded. will be deleted only then.

If there is no expansion, nNumUsed will always increase, so its value is not a valid number of elements. nNumOfElements is the number of valid elements in the array, so nNumOfElements ≤ nNumUsed.

Bucket

The structure stores the key and value of the element. And h is the hash code: if the key is a numerical value (and numerical index), then its value is the value of the numerical index; if the key is a string, then its value is the hash value calculated by the Time33 algorithm based on the string key. The h value is used to map the storage location of the element.

Array implementation:

In order to achieve the orderliness of the hash table, the hash table in PHP adds an element between the hash function and the element array. Layer mapping table, this mapping table is also an array, the size is the same as the array storing elements.

The intermediate mapping table stores the subscripts of elements in the actual stored ordered array: elements are inserted into the actual storage array in order, and then their array subscripts are stored in the location hashed by the hash function. in the newly added mapping table.

Hash function:

The storage location of the element is mapped according to the key. The modulo is usually used as the hash function: key->h %nTableSize. But PHP takes another approach: nIndex = key->h | nTableMask.

This intermediate mapping table is not found in the structure of the PHP array. In fact, it is placed together with arData. When the array is initialized, the memory used to store the Bucket is allocated and the same amount of space of uint32_t size is allocated at the same time. Then offset arData to the location where the array of elements is stored.

The intermediate mapping table can be accessed forward through arData.

Hash conflict:

Different key values may calculate the same hash value, and conflicts will occur when inserting into the hash table, because the mapping table Only one element can be stored.

Solution: String the conflicting Buckets into a linked list, that is, the intermediate mapping table maps out a Bucket linked list, not a Bucket. When searching, you need to traverse this linked list and compare keys one by one to find the target element.

HashTable will record the storage location of the elements that conflict with it in the arData array.

When setting the mapping value, it is found that the position to be set in the intermediate mapping table has been occupied by the previously inserted element (the value is not equal to the initialized -1), then it will Save the existing value to the newly inserted Bucket (that is, u2.next=0 after c is inserted), and then update the value in the mapping table to the storage location of the new Bucket (that is, the value in the mapping table: 2).

Reference:

A reference is a structure that points to other types, similar to the concept of pointers in C language. When a reference type variable is modified, the modification will be reflected in the actual referenced variable.

In PHP, a reference variable is generated through the & operator, such as $b = &$a. During execution, a zend_reference structure is first allocated to the variable of the & operation. This structure is a reference type structure. A zval is embedded, and the value of this zval points to the value of the original zval. Then the type of the original zval is modified to IS_REFERENCE, and the value of the original zval points to the newly created zend_reference structure.

Example:


$a = date("Y-m");$b = &$a;

$a is a string, through & $a converts it into a reference type and assigns it to $b. After conversion, the type of $a changes from IS_STRING to IS_REFERENCE, and the value of $a is also converted into a zend_reference structure, which points to the original string.

$a and $b indirectly point to the actual value.

You need to pay attention when using references. References can only be generated through & and cannot be passed through assignment.

As in the above example, if $b is assigned to other variables, then the value passed to the new variable will be the value of the actual reference, not the reference itself.


$a = date("Y-m");$b = &$a;$c = $b; //如果想让$c也引用指向$a/$b引用的值,则:$c = &$b

Recommended course:PHP video tutorial

The above is the detailed content of [PHP Learning] Data Types of PHP7. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete