Let's say I define an array:
$a = Array();
$a[0] = 1;
$a['0'] = 2;
$a['0a'] = 3;
print_r($a);
The output is fairly expected. I assume that there is some sort of strval() or intval() going on internally to allow the value to be overwritten.
Array
(
[0] => 2
[0a] => 3
)
But this is what confuses me.
foreach($a as $k => $v) {
print(gettype($k) . "\n");
}
This gives me:
integer
string
How is it that PHP internally makes those type conversions when using Array keys? i.e., How does it determine the key is a "valid decimal integer" as per the documentation? Also, is_int doesn't work on strings.
0 == '0'but0 !== '0'. In the case of arrays$a['key']can be written as$a[key]you'll just get an undefined constant warning, which unfortunately can be suppressed.