php 数组行为奇怪,键值 07 &08 [英] php array behaving strangely with key value 07 & 08
问题描述
我有几个月的数组
$months[01] = 'January';
$months[02] = 'February';
$months[03] = 'March';
$months[04] = 'April';
$months[05] = 'May';
$months[06] = 'June';
$months[07] = 'July';
$months[08] = 'August';
$months[09] = 'September';
$months[10] = 'October';
$months[11] = 'November';
$months[12] = 'December';
现在数组没有为键 07
输出正确的值 &08
.
Now the array does not output correct value for key 07
& 08
.
尝试执行 print_r($months)
你不会得到任何键值 August
和 September
的零键索引.
Try doing print_r($months)
you will not get any key value August
and zero key index for September
.
虽然我可以通过删除前导零来解决问题,但我仍然很想知道相同的原因.
Though I’m able to solve the problem by removing the leading zero, still I would love to know the reason for same.
即使是 PHP 编辑器也发现了一些问题,但无法说出问题所在.
Even the PHP editor spots some problem but unable to tell what is the problem.
谢谢
推荐答案
在数字前添加 0 意味着 PHP 将其解析为八进制值,就像添加 0x 导致它被解析为十六进制值一样.删除零,它会正常工作.
Prepending 0 before a number means PHP parses it as an octal value in the same way that prepending 0x causes it to be parsed as a hexadecimal value. Remove the zero, and it will work fine.
echo 07; // prints 7
echo 010; // prints 8
这主要用于指定 unix 权限时:
This is mostly used when specifying unix permissions:
chmod("myfile", 0660);
除此之外,您很少会想做.
Except for that it's rarely something that you'd want to do.
这在 PHP 手册中有所描述.
这篇关于php 数组行为奇怪,键值 07 &08的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!