OK, we all know, all is wrong with PHP. It’s a language where main programming style is hack/patch, and that’s also the way it’s organized. Where else do you have functions that in one variant have certain order of parameters and in other reversed?… OK, in quite a few, but that’s not the point.
I wanna talk about arrays; probably the most commonly used structured type in all languages.
My main shock was the following feature/bug – PHP converts array keys from string to int if it “thinks” it’s good to do (don’t ask me why, it’s way over my head). But it does it in the weirdest possible way, let’s look at the type of the keys:
$ar = array(); $ar["2147483647"] = 0; $ar["2147483648"] = 0; $ar["2999999999"] = 0; $ar["3000000000"] = 0; foreach ($ar as $k => $v) var_dump($k);
What this outputs (at least on 32b systems, verified with 5.2.4 and 5.3.1 “stable” releases):
int(2147483647) int(-2147483648) int(-1294967297) string(10) "3000000000"
WHY the gap?
What goes on here is quite obvious – PHP checks if all characters are numbers, then it checks string length, if it’s 10, then it checks first character if it’s 3 and more, or 2 and less.
Why would anyone do all this work to save just a few bytes? Esp. when you consider that PHP uses about 60-70 bytes to store a single array element with integer key and integer value! And why not right away compare the string with string “2147483647” one by one character, to avoid the overflow?!