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.
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:
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:
Hash function:
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:
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!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools