2
function getMonths() {
        $data[] = array('key' => 01, 'name' => 'January');
        $data[] = array('key' => 02, 'name' => 'February');
        $data[] = array('key' => 03, 'name' => 'March');
        $data[] = array('key' => 04, 'name' => 'April');
        $data[] = array('key' => 05, 'name' => 'May');
        $data[] = array('key' => 06, 'name' => 'June');
        $data[] = array('key' => 07, 'name' => 'July');
        $data[] = array('key' => 08, 'name' => 'August');
        $data[] = array('key' => 09, 'name' => 'September');
        $data[] = array('key' => 10, 'name' => 'October');
        $data[] = array('key' => 11, 'name' => 'November');
        $data[] = array('key' => 12, 'name' => 'December');
        error_log("data:");
        error_log(print_r($data,1));

        return $data;
    }

For some reason I can't fathom, August and September's keys are 0 when I print_r the $data array, instead of 08 and 09.

[03-Jan-2014 15:28:26] data:
[03-Jan-2014 15:28:26] Array
(
    [0] => Array
        (
            [key] => 1
            [name] => January
        )

    [1] => Array
        (
            [key] => 2
            [name] => February
        )

    [2] => Array
        (
            [key] => 3
            [name] => March
        )

    [3] => Array
        (
            [key] => 4
            [name] => April
        )

    [4] => Array
        (
            [key] => 5
            [name] => May
        )

    [5] => Array
        (
            [key] => 6
            [name] => June
        )

    [6] => Array
        (
            [key] => 7
            [name] => July
        )

    [7] => Array
        (
            [key] => 0
            [name] => August
        )

    [8] => Array
        (
            [key] => 0
            [name] => September
        )

    [9] => Array
        (
            [key] => 10
            [name] => October
        )

    [10] => Array
        (
            [key] => 11
            [name] => November
        )

    [11] => Array
        (
            [key] => 12
            [name] => December
        )

)

What am I missing here?

1
  • can you use the key as strings? Commented Jan 3, 2014 at 21:34

4 Answers 4

2

Literals beginning 0 are octal literals. Where decimal is a numeric system with 10 digits, octal is a numeric system with only 8 digits. 08, 09 etc are therefore numbers that do not exist. An analogous example is the non-existent hexadecimal literal 0xG.

Just write 8 and 9 like a normal person ;)

Incidentally, that seems like a rather strange way to use an array. Why not this:

function getMonths()
{
    return Array(
        1  => 'January',
        2  => 'February',
        // ...
        12 => 'December'
    );
}

If you are interfacing with some external system that requires a specific representation of those keys, where that specific representation is a string with a leading zero, then use strings:

function getMonths()
{
    return Array(
        '01' => 'January',
        '02' => 'February',
        // ...
        '12' => 'December'
    );
}
Sign up to request clarification or add additional context in comments.

5 Comments

I'm not sure what you mean by, "like a normal person." Do you mean without the leading 0? Not possible; the point of sale system that this data gets posted to requires leading zeroes for single-digit months.
@EmmyS: Huh? No it doesn't! Your keys are integers so the string representation doesn't even exist. If you need strings then use them, but the moment you sit on integer literals you promise to abide by the rules of the language's integer-to-string conversion rules.
"like a normal person" was epic :)
@LightnessRacesinOrbit - wow, that was rude. I asked an honest question; I don't see the need for snark. I've been working with PHP for a number of years and never run into this issue. And to respond: yes, it does. The new third-party POS does require two-digit months to be sent in. So do you think, in your infinite wisdom, you could please just explain what you mean by "like a normal person"? Without being an ass about it?
@EmmyS: There was no snark. No, your third-party POS does not require two-digit integer literals because this makes no sense. They are a part of your source code and do not even exist at runtime. Did you read the second part of my answer where I address this concern? After using PHP for a number of years I would expect you to know the difference between a string and an integer.
2

You need to put them in quotes. '08', '09', etc. Otherwise PHP thinks you are using Octal numbers.

Octal: http://en.wikipedia.org/wiki/Octal

PHP Ints: https://www.php.net/manual/en/language.types.integer.php

1 Comment

Explanation of why it happens might be useful
1

Numbers starting with zero (0) are octal in PHP, so 08 and 09 are invalid numbers, check out more: http://www.php.net/manual/en/language.types.integer.php

to fix: remove leading zeros or enclose in single quotes

Comments

0

Here is an alternative to your function

function getMonths() {
    for ($x=0; $x < 12; $x++) {

        $time = strtotime('+' . $x . ' months', strtotime(date('Y-M' . '-01')));
        $key = date('m', $time);
        $name = date('F', $time);
        $months[$key] = $name;
    }
    return $months ;
}

output is as

Array ( [01] => January [02] => February [03] => March [04] => April [05] => May [06] => June [07] => July [08] => August [09] => September [10] => October [11] => November [12] => December )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.