Is this a bug in PHP? [duplicate]

不问归期 提交于 2019-12-18 09:08:43

问题


Possible Duplicate:
php array behaving strangely with key value 07 & 08

I've found something weird in PHP, if I use numeric arrays the 8th array gets ignored, here when I put 'Cherry' into $fruit[08], php seams to step over it.

What's going on ? Is this a bug or something else.

<pre>
<?php

$fruit[01] = "Apples";
$fruit[02] = "Pears";
$fruit[03] = "Bananas";
$fruit[04] = "Grape";
$fruit[05] = "Orange";
$fruit[06] = "Peach";
$fruit[07] = "Lemon";
$fruit[08] = "Cherry";
$fruit[09] = "Mango";

print_r($fruit);

?>
</pre>

Output:

Array
(
    [1] => Apples
    [2] => Pears
    [3] => Bananas
    [4] => Grape
    [5] => Orange
    [6] => Peach
    [7] => Lemon
    [0] => Mango
)

回答1:


Your indices are being treated as octal numbers because of the leading zeroes.

08 and 09 will both be evaluated as zero, so your last entry ("Mango") ends up in array index 0.




回答2:


08 is treated as octal.

Don't use leading zeros.

For that matter, don't use explicit indices for creating arrays:

$fruit = array(
           "Apples",
           "Pears",
           etc
         );

(or for PHP 5.4 and newer):

$fruit = [
           "Apples",
           "Pears",
           etc
         ];



回答3:


Because you typed 08 rather than 8 PHP thinks you are talking about a value that doesn't exist. In octal numbers run from 00 to 07. To let PHP take care of the values for you type:

<pre>
<?php

$fruit[] = "Apples";
$fruit[] = "Pears";
$fruit[] = "Bananas";
$fruit[] = "Grape";
$fruit[] = "Orange";
$fruit[] = "Peach";
$fruit[] = "Lemon";
$fruit[] = "Cherry";
$fruit[] = "Mango";

print_r($fruit);

?>
</pre>

In short it's not a bug but it is one of those things that will catch you out if you were not expecting it.




回答4:


You are using octal notation for the array indices. 09 and 09 are invalid octal numbers (8 decimal is 010 in octal notation and 9 decimal is 011 in octal notation). Invalid numbers are evaluated to 0, so "Cherry" will be overidden with "Mango".




回答5:


Numeric arrays don't need leading zeroes. If this is causing problems for you, remove all leading zeroes from the indices. Also, you can append elements to an array using $array[] instead of actually giving an index.




回答6:


PHP treats integers as octals instead of decimals when they start with a zero. Try using proper indices, e.g. 1, 2, 3 instead of 01, 02, 03.

BTW, in PHP arrays start at 0.




回答7:


PHP is treating your array indices as octal instead of decimal numbers because of the leading '0's. Remove those and everything should work fine!

Also, you may well know this, but just in case you don't, array indices start at 0, not 1.




回答8:


This is because you are using 01, 02, 03 and so on (leading zero). For PHP, these are octal numbers. And octal numbers just range from 0-7 (so 08 and 09 are in fact illegal numbers).

Cut the leading zero and everything is fine ;)



来源:https://stackoverflow.com/questions/11416046/is-this-a-bug-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!