Home Backend Development PHP Tutorial The composition and types of PHP variables

The composition and types of PHP variables

Jun 05, 2018 pm 04:44 PM
php kernel

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;     //存储变量的类型
}
Copy after login

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;
Copy after login

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;
Copy after login

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;
Copy after login

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);
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

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...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles