This article mainly introduces the composition and types of PHP variables. Interested friends can refer to it. I hope it will be helpful to everyone.
php variable components:
Variable name: The variable name in PHP language starts with $ in English/underscore, and can contain numbers, underscores, Letters, case sensitive. At the same time, PHP also supports compound variables, in the shape of $$A, which increases the dynamics of PHP.
Type: PHP is a weakly typed language and can assign any type of value.
Content: There can only be one value at the same time.
There are 8 data types in the PHP language, divided into three major categories:
1. Scalar types: Boolean, integer, float, string;
2. Composite types: object, array;
3. Special types: NULL, resource;
As a weakly typed language, PHP implements all Variables store data through the structure zval, which not only contains the value of the variable, but also the type of the variable, which is the core of PHP's weak typing.
zval data structure:
struct _zval_struct{ zvalue_value value; //存储变量的值 zend_unint refcount_gc; //引用计数 zend_char is_ref_gc; // 是否为引用 zend_char type; //存储变量的类型 }
Zvalue_value is not a structure. In order to save memory, it is implemented using union, because the variable can only represent one type at the same time. Its prototype:
typedef union _zvalue_value{ long lval; double dval; struct { char *val; int len; //字符串的长度 }str; HashTable *ht; //保存数组 zend_object_value obj; //对象 }zvalue_value;
Hash table:
Many implementations inside PHP are based on hash tables: variable scope, function table, class attributes, methods, etc. A lot of data inside the Zend engine is stored in hash tables.
php arrays use hash tables to store associated data. The hash table implementation uses two data structures, HashTable and Bucket:
HashTable:
typedef struct _hashtable { uint nTableSize; // hash Bucket的大小,最小为8,以2x增长。 uint nTableMask; // nTableSize-1 , 索引取值的优化 uint nNumOfElements; // hash Bucket中当前存在的元素个数,count()函数会直接返回此值 ulong nNextFreeElement; // 下一个数字索引的位置 Bucket *pInternalPointer; // 当前遍历的指针(foreach比for快的原因之一) Bucket *pListHead; // 存储数组头元素指针 Bucket *pListTail; // 存储数组尾元素指针 Bucket **arBuckets; // 存储hash数组 dtor_func_t pDestructor; // 在删除元素时执行的回调函数,用于资源的释放 zend_bool persistent; // 指出了Bucket内存分配的方式。如果persisient为TRUE, 则使用操作系统本身的内存分配函数为Bucket分配内存,否则使用 PHP的内存分配函数。 unsigned char nApplyCount; // 标记当前hash Bucket被递归访问的次数(防止多次递归) zend_bool bApplyProtection;// 标记当前hash桶允许不允许多次访问,不允许时,最多只能递归3次 #if ZEND_DEBUG int inconsistent; #endif } HashTable;
The expansion of capacity in HashTable is always adjusted to the integer power of 2 close to the initial size. Because:
When selecting slots, the & operation is used instead of modulo. This is because the modulo operation is relatively more expensive than the bitwise AND operation. The function of the mask is to map the hash value to the index range that the slot can store. For example: the index value of a certain key is 21, the size of the hash table is 8, then the mask is 7, and the binary representation of the sum is: 10101 & 111 = 101, which is 5 in decimal. Because the binary system of 2 to the power of an integer -1 is special: the values of the next N bits are all 1, which makes it easier to map the values. If it is an ordinary number and is combined in binary, it will affect the result of the hash value. Then the average distribution of values calculated by the hash function may be affected.
bucket:
typedef struct bucket { ulong h; // 对char *key进行hash后的值,或者是用户指定的数字索引值 uint nKeyLength; // hash关键字的长度,如果数组索引为数字,此值为0 void *pData; // 指向value,一般是用户数据的副本,如果是指针数据,则指向pDataPtr void *pDataPtr; //如果是指针数据,此值会指向真正的value,同时上面pData会指向此值 struct bucket *pListNext; // 整个hash表的下一元素 struct bucket *pListLast; // 整个哈希表该元素的上一个元素 struct bucket *pNext; // 存放在同一个hash Bucket内的下一个元素 struct bucket *pLast; // 同一个哈希bucket的上一个元素 // 保存当前值所对于的key字符串,这个字段只能定义在最后,实现变长结构体 char arKey[1]; } Bucket;
The hash value is stored in the Bucket instead of the hash index.
The last field of the above structure is used to save the key string, but this field is declared as an array of only one character. In fact, this is a common variable-length structure. The main purpose is Increase flexibility. The following is the code for applying for space when inserting new elements into the hash table
p = (Bucket *) pemalloc(sizeof(Bucket) - 1 + nKeyLength, ht->persistent); if (!p) { return FAILURE; } memcpy(p->arKey, arKey, nKeyLength);
Insertion process diagram
Hash algorithm
The hash function in php is implemented using the DJBX33A algorithm.
Object:
php objects are stored using the data structure zend_object_value;
Summary: The above is the entire content of this article, I hope it can be helpful to everyone learning helps.
Related recommendations:
Analysis of CURL examples of data transmission in PHP
Sharing of simple examples of adding data to xml files in PHP
Example of website directory scanning index tool based on PHP
The above is the detailed content of The composition and types of PHP variables. 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