According to the PHP Manual
The key can either be an integer or a string. The value can be of any type.
Additionally the following key casts will occur:
- Strings containing valid decimal integers, unless the number is preceded by a + sign, will be cast to the integer type. Eg the key "8" will actually be stored under 8. On the other hand "08" will not be cast, as it isn't a valid decimal integer.
As per about quotes I wrote following code. In below code the key +8 is getting cast to the integer type. How this is possible as the above rule says it should not happen?
<?php
$array = array(
+8 => "a"
);
var_dump($array);
?>
Output :
array(1) {
[8]=>
string(1) "a"
}
Because +8
is an integer literal and, as such, the + sign is implicit and it makes no difference to add it or not:
var_dump(+8, 8);
int(8) int(8)
Nothing in the docs state that PHP will cast integers to strings. I think you just misread this sentence (emphasis mine):
Strings containing valid decimal integers, unless the number is preceded by a + sign
$array = array(
7 => 'a',
+8 => 'b',
'+9' => 'c',
);
var_dump(array_keys($array));
array(3) { [0]=> int(7) [1]=> int(8) [2]=> string(2) "+9" }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.