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.
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:
The Code is as follows:
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 ++; // faster than * 33
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... * // The remaining characters are hashed here
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 the hash value.
}
Ps: The following functions are still unknown:
Why does hash = 5381 be set?
Is this step = 8 loop for efficiency?