0

Is this a bug?

This example should return 000001 but returns 000019.

What am I doing wrong?

This array is becouse this file is load intro the class method;

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];
?>
3
  • 1
    in my php version this throws a parse error (since php 7.0), because the 0 prefix is octal notation as explained in the answer from Marc B, and 8 and 9 are not allowed in octal. Commented Jul 19, 2016 at 17:25
  • For further reference, see PHP: Integers. Commented Jul 19, 2016 at 17:26
  • Try echo 0000015; vs. echo '0000015'; and the issue should become clearer. Yes, the overall answer is those meddling octals. Try $str['000015'] = "000015"; and use echo $str['000015']; to get the result you expect. Commented Jul 19, 2016 at 17:44

1 Answer 1

5

Numbers prefixed with 0 are treated as octal. Octal 17 -> decimal 15.

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

which means $array[019] is effectively identical to $array[1], because 019 -> 1, as it's an invalid octal number, and the 9 is stripped.

Sign up to request clarification or add additional context in comments.

1 Comment

I had not thought of that. Thanks =)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.