[英]How the numerical key of an array preceding with '+' sign is casted into integer type?
According to the PHP Manual 根据PHP手册
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. 例如,键“ 8”实际上将存储在8以下。另一方面,“ 08”将不是强制转换,因为它不是有效的十进制整数。
As per about quotes I wrote following code. 根据关于报价我写了以下代码。 In below code the key +8 is getting cast to the integer type. 在下面的代码中,键+8被强制转换为整数类型。 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: 因为+8
是整数文字,因此+号是隐式的,添加或不添加都没有区别:
var_dump(+8, 8);
int(8) int(8)
Nothing in the docs state that PHP will cast integers to strings. 文档中没有任何内容表明PHP会将整数转换为字符串。 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" }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.