Data types in PHP7
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
## 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.
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 when the hash function stores the location based on the hash code element of the key. 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.
BucketThe 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 , convert it into a reference type through &$a and assign 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
The above is the detailed content of Data types in PHP7. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to install the mongo extension in php7.0: 1. Create the mongodb user group and user; 2. Download the mongodb source code package and place the source code package in the "/usr/local/src/" directory; 3. Enter "src/" directory; 4. Unzip the source code package; 5. Create the mongodb file directory; 6. Copy the files to the "mongodb/" directory; 7. Create the mongodb configuration file and modify the configuration.

In php5, we can use the fsockopen() function to detect the TCP port. This function can be used to open a network connection and perform some network communication. But in php7, the fsockopen() function may encounter some problems, such as being unable to open the port, unable to connect to the server, etc. In order to solve this problem, we can use the socket_create() function and socket_connect() function to detect the TCP port.

To resolve the plugin not showing installed issue in PHP 7.0: Check the plugin configuration and enable the plugin. Restart PHP to apply configuration changes. Check the plugin file permissions to make sure they are correct. Install missing dependencies to ensure the plugin functions properly. If all other steps fail, rebuild PHP. Other possible causes include incompatible plugin versions, loading the wrong version, or PHP configuration issues.

Common solutions for PHP server environments include ensuring that the correct PHP version is installed and that relevant files have been copied to the module directory. Disable SELinux temporarily or permanently. Check and configure PHP.ini to ensure that necessary extensions have been added and set up correctly. Start or restart the PHP-FPM service. Check the DNS settings for resolution issues.

How to install and deploy php7.0: 1. Go to the PHP official website to download the installation version corresponding to the local system; 2. Extract the downloaded zip file to the specified directory; 3. Open the command line window and go to the "E:\php7" directory Just run the "php -v" command.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...

Compared with PHP7, PHP8 has some advantages and improvements in terms of performance, new features and syntax improvements, type system, error handling and extensions. However, choosing which version to use depends on your specific needs and project circumstances. Detailed introduction: 1. Performance improvement, PHP8 introduces the Just-in-Time (JIT) compiler, which can improve the execution speed of the code; 2. New features and syntax improvements, PHP8 supports the declaration of named parameters and optional parameters, making functions Calling is more flexible; anonymous classes, type declarations of properties, etc. are introduced.
