PHP kernel exploration variable, _ PHP Tutorial

Source: Internet
Author: User
Tags key string
PHP kernel exploration variable ,. PHP kernel exploration variables. php variable components: variable name: the variable name in the php language starts with $ + English underline, can contain numbers, underscores, letters, case sensitive. At the same time, PHP kernel exploration variables,

Php variable components:

Variable name: php variable name starts with $ + English/underline, can contain numbers, underscores, letters, case sensitive. PHP also supports compound variables, such as $ A, to increase the dynamic nature of php.

Type: php is a weak language and can assign values of any type.

Content: there can only be one value at a time.

There are eight data types in php, which are divided into three categories:

1. scalar type: Boolean, integer, float, string;

2. Composite type: object, array;

3. Special type: NULL, resource;

As a weak type language, php stores data through the structure zval in all internal variables, including not only the value of the variable, but also the type of the variable, is the core of the weak php type.

Zval data structure:

Struct _ zval_struct {zvalue_value value; // zend_unint refcount_gc; // reference count zend_char is_ref_gc; // whether zend_char type is referenced; // type of the storage variable}

Zvalue_value is not a struct. it is implemented by union to save memory usage, because variables can only represent one type at a time. Prototype:

Typedef union _ zvalue_value {long lval; double dval; struct {char * val; int len; // string length} str; HashTable * ht; // Save the array zend_object_value obj; // object} zvalue_value;

Hash table:

Many implementations of php are based on hash tables: the scope of variables, function tables, class attributes, and methods. many data in the Zend Engine is stored in the hash table.

The php array uses a hash table to store associated data. the hash table uses two data structures: HashTable and Bucket:

HashTable:

Typedef struct _ hashtable {uint nTableSize; // size of the hash Bucket. the minimum value is 8, which is increased by 2x. Uint nTableMask; // nTableSize-1, optimized uint nNumOfElements; // number of existing elements in the hash Bucket, the count () function will directly return this value ulong nNextFreeElement; // Bucket * pInternalPointer; // The pointer Currently traversed (one of the reasons why foreach is faster than for) Bucket * pListHead; // stores the array header element pointer Bucket * pListTail; // store the array end element pointer Bucket ** arBuckets; // store the hash array dtor_func_t pDestructor; // The callback function executed when the element is deleted, used to release zend_bool persistent resources; // specifies the Bucket memory allocation method. If persisient is TRUE, use the memory allocation function of the operating system to allocate memory to the Bucket. Otherwise, use the PHP memory allocation function. Unsigned char nApplyCount; // indicates the number of recursive accesses to the current hash Bucket (preventing multiple recursion) zend_bool bApplyProtection; // indicates that the current hash Bucket cannot be accessed multiple times, only three recursion times at most # if ZEND_DEBUG int inconsistent; # endif} HashTable;

The capacity expansion in HashTable is always adjusted to the integer power of 2 that is close to the initial size. Because:

When selecting a slot, use & operation instead of modulo operation. this is because the consumption of the modulo operation and the bitwise operation are much larger. The role of mask is to map the hash value to the index range that the slot can store. For example, if the index value of a key is 21 and the hash table size is 8, then the mask is 7, and the binary representation of the result is: 10101 & 111 = 101 is the decimal 5. Because the binary value of the integer-1 of 2 is special: the values of the next N bits are all 1, which makes it easier to map values, if a common number is binary and then the hash value is affected. The average distribution of hash function values may be affected.

Bucket:

Typedef struct bucket {ulong h; // the hash value of the char * key, or the user-specified digital index value uint nKeyLength; // The length of the hash keyword, if the array index is a number, this value is 0 void * pData; // point to value, which is generally a copy of user data. if it is pointer data, it points to pDataPtr void * pDataPtr; // if it is pointer data, this value will point to the real value, and above pData will point to this value struct bucket * pListNext; // the next element of the entire hash table, struct bucket * pListLast; // the previous element, struct bucket * pNext, of the entire hash table; // the next element struct Bucket * pLast stored in the same hash bucket; // the previous element of the same hash bucket // The key string that stores the current value, this field can only be defined at the end, implementing the variable-length structure char arKey [1];} Bucket;

The Bucket stores the hash value instead of the hash index.

The last field of the struct above is used to save the string of the key, but this field is declared as an array with only one character. In fact, it is a long variable length struct, the main purpose is to increase flexibility. The following code applies for space when a new element is inserted into the hash table

p = (Bucket *) pemalloc(sizeof(Bucket) - 1 + nKeyLength, ht->persistent);if (!p) {  return FAILURE;}memcpy(p->arKey, arKey, nKeyLength);

Insert 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;

Articles you may be interested in:
  • Php design mode Interpreter (Interpreter mode)
  • In-depth analysis of the interpreter mode in PHP design mode
  • PHP kernel exploration: usage instructions on variable storage and types
  • PHP kernel exploration: variable overview
  • PHP kernel exploration: hash table collision attack principle
  • PHP kernel exploration-interpreter execution process

Php variable component: variable name: php variable name starts with $ + English/underline, can contain numbers, underscores, letters, case sensitive. At the same time...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.