In PHP, the hash function of Array is implemented. today we have reviewed and learned how to implement variables in PHP. by browsing its source code, we found that all data types in PHP are stored through a union. Php is a weak type language. in its implementation, it manages variables by recording their types and values .? Use the most Array hash function in PHP to implement
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:
Ps: the following functions are still unknown:
1 .? Why does hash = 5381 be set?
2 .? Is this step = 8 loop for efficiency?
?
Static inline ulong zend_inline_hash_func (const char * arKey, uint nKeyLength) {register ulong hash = 5381 ;????????????????????????? // Is there any xuanjicang in the initial value settings? /* 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}?