[英]php array behaving strangely with key value 07 & 08
I have an array for months 我有几个月的阵列
$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';
Now the array does not output correct value for key 07
& 08
. 现在,数组不会为键
07
和08
输出正确的值。
Try doing print_r($months)
you will not get any key value August
and zero key index for September
. 尝试执行
print_r($months)
你将无法获得August
任何关键值和September
零关键指数。
Though I'm able to solve the problem by removing the leading zero, still I would love to know the reason for same. 虽然我能够通过移除前导零来解决问题,但我仍然想知道相同的原因。
Even the PHP editor spots some problem but unable to tell what is the problem. 即使PHP编辑器发现了一些问题,但无法分辨出问题所在。
Thanks 谢谢
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. 在数字前面加0意味着PHP将其解析为八进制值,其方式与前置0x相同,使得它被解析为十六进制值。 Remove the zero, and it will work fine.
删除零,它将正常工作。
echo 07; // prints 7
echo 010; // prints 8
This is mostly used when specifying unix permissions: 这主要在指定unix权限时使用:
chmod("myfile", 0660);
Except for that it's rarely something that you'd want to do. 除此之外,它很少是你想做的事情。
This is described in the PHP Manual . 这在PHP手册中有描述。
Generally, putting a leading 0 on a number tells the compiler that you've giving an octal number (base 8, not base 10). 通常,在数字上放置前导0会告诉编译器您给出了一个八进制数(基数为8,而不是基数为10)。
In octal, 8 and 9 don't exist (8 is 010, 9 is 011), so you're confusing php. 在八进制中,8和9不存在(8是010,9是011),所以你混淆了php。
If you really want a leading zero, you can use strings for your indexes 如果您确实需要前导零,则可以为索引使用字符串
PHP会将前导0的数字视为八进制数,或者删除前导0或将键值放在引号中。
The way you form an integer literal is important. 形成整数文字的方式很重要。
See the structure for decimal literals? 查看十进制文字的结构? Notice how a preceeding zero is not part of that pattern?
注意前一个零如何不是该模式的一部分?
Either you have to remove the zeros, or treat your array keys as strings 要么必须删除零,要么将数组键视为字符串
$months['01'] = 'January';
$months['02'] = 'February';
// etc...
I just realized this after precious minutes of debugging and table head banging. 我只是在经过宝贵的调试和桌面敲击之后才意识到这一点。
The problem is that PHP doesn't raise an error or even a warning about having malformed the octal literal integer. 问题是PHP不会引发错误甚至是关于八进制文字整数格式错误的警告。 It just ignores the part from the error until the end of the literal.
它只是忽略了从错误到文字结尾的部分。
Damn. 该死的。
PS: Who uses octal numbers? PS:谁使用八进制数? never did, never heard of someone using them for a good reason.
从来没有,从来没有听说有人使用它们是有充分理由的。
Hexadecimal is great, but octal? 十六进制是伟大的,但八进制? damn.
该死的。
Even for permission I usually do chmod ugo+rwx
is more straightforward. 即使是获得许可我通常也会使用
chmod ugo+rwx
更直接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.