索引000001中的php数组错误

时间:2016-07-19 17:19:38

标签: php arrays

这是一个错误吗?

此示例应返回000001但返回000019。

我做错了什么?

这个数组是因为这个文件是加载intro类方法;

PHP Version 5.6.19。

XAMPP for Windows 5.6.19

<?
    global $str; 
    $str = array();
    $str[000000] =  "000000";
    $str[000001] =  "000001";
    $str[000002] =  "000002";
    $str[000003] =  "000003";
    $str[000004] =  "000004";
    $str[000005] =  "000005";
    $str[000006] =  "000006";
    $str[000007] =  "000007";
    $str[000008] =  "000008";
    $str[000009] =  "000009";
    $str[000010] =  "000010";
    $str[000011] =  "000011";
    $str[000012] =  "000012";
    $str[000013] =  "000013";
    $str[000014] =  "000014";
    $str[000015] =  "000015";
    $str[000016] =  "000016";
    $str[000017] =  "000017";
    $str[000018] =  "000018";
    $str[000019] =  "000019";
    $str[000020] =  "000020";


    echo $str[000001];
?>

1 个答案:

答案 0 :(得分:5)

0为前缀的数字被视为八进制。 Octal 17 - &gt;十进制15。

echo 01 -> 1
echo 02 -> 2
...
echo 07 -> 7
echo 08 -> 0  (failure)
echo 09 -> 0  (failure)
echo 019 -> 1

这意味着$array[019]实际上与$array[1]相同,因为019 - &gt; 1,因为它是一个无效的八进制数,并且9被删除。