Today, I reviewed and learned how to implement variables in PHP. by browsing its source code, I found that all data types in PHP are stored through a union. The php language is a weak type language, which is managed by recording the type and value of the variable.
The most commonly used non-Array in PHP does not belong to. How is Array implemented? In PHP, the internal Array is implemented through a hashtable, And the hash conflict is solved using the link method. In this case, the complexity of searching Array elements is O (N ), preferably 1.
The method for calculating the string hash value is as follows:
Static inline ulong zend_inline_hash_func (const char * arKey, uint nKeyLength) {register ulong hash = 5381; // is there any xuanjicang for the initial value? /* Variant with the hash unrolled eight times */for (; nKeyLength> = 8; nKeyLength-= 8) {// Why is this step = 8? Hash = (hash <5) + hash) + * arKey ++; hash = (hash <5) + hash) + * arKey ++; hash = (hash <5) + hash) + * arKey ++; hash = (hash <5) + hash) + * arKey ++; // Fast hash = (hash <5) + hash) + * arKey ++; hash = (hash <5) + hash) + * arKey ++; hash = (hash <5) + hash) + * arKey ++; hash = (hash <5) + hash) + * arKey ++;} switch (nKeyLength) {case 7: hash = (hash <5) + hash) + * arKey ++;/* fallthrough... * // hash case 6: hash = (hash <5) + hash) + * arKey ++;/* fallthrough... */case 5: hash = (hash <5) + hash) + * arKey ++;/* fallthrough... */case 4: hash = (hash <5) + hash) + * arKey ++;/* fallthrough... */case 3: hash = (hash <5) + hash) + * arKey ++;/* fallthrough... */case 2: hash = (hash <5) + hash) + * arKey ++;/* fallthrough... */case 1: hash = (hash <5) + hash) + * arKey ++; break; case 0: break; EMPTY_SWITCH_DEFAULT_CASE ()} return hash; // return hash value}
Ps: The following functions are still unknown:
- Why does hash = 5381 be set?
- Is this step = 8 loop for efficiency?