-1

I am having trouble with some code:

else if ($IDKood[3] . $IDKood[4] == 08) {
            $Month = "August";
        }
        else if ($IDKood[3] . $IDKood[4] == 09) {
            $Month = "September";
        }

Essentially If the 2 numbers are equal to a given number I want it to output a month, this leads up to all months. Everything works well upto the number 08 and 09 and I can't seem to fix it. As I understand this has something to do with php itself, I tried to put the numbers in a string and integer to no avail.

J.Ace
  • 11
  • 4
  • change `08` to `8`. _or_ what could `$IDKood[3] . $IDKood[4]` result to? – Jeff Oct 07 '17 at 22:17
  • Can you check this echo $IDKood[3] . $IDKood[4] and what gives you – Ferhat BAŞ Oct 07 '17 at 22:19
  • if you're comparing *numbers*, don't preceed the numbers with `0` or it is interpreted as octal (and 8/9 is not allowed in octal). if you're comparing strings, put `08`in quotes like '08' – Jakumi Oct 07 '17 at 22:19
  • Also you are using string opreand "." And but you dont compare the string 08 instead of "08" – Ferhat BAŞ Oct 07 '17 at 22:20
  • and seeing this snipplet there might be a good chance for code-optimisation... Think about using arrays for that. – Jeff Oct 07 '17 at 22:20

2 Answers2

1

An integer literal in PHP is in octal if it starts with a leading zero. Knock off the leading zero and you'll be able to go above seven!

From the manual:

To use octal notation, precede the number with a 0 (zero).

Also, you seem to be comparing strings with numbers. If $IDKood is an array of characters, then perhaps you should be doing a string comparison:

if ($IDKood[3] . $IDKood[4] == '09') {

..though as ishegg observes, as long as you knock the leading zero off, the implicit conversion from string to integer will work fine:

if ($IDKood[3] . $IDKood[4] == 9) {

Having said all that, if you're just converting a number to a month name, there may be better ways to do it.

 $dateObj   = DateTime::createFromFormat('!m', (int) ($IDKood[3] . $IDKood[4]));
 $monthName = $dateObj->format('F');
Matt Gibson
  • 37,886
  • 9
  • 99
  • 128
  • ..a classical case of an XY Problem this is. – Jeff Oct 07 '17 at 22:31
  • [It doesn't need to be `"09"`](https://3v4l.org/9VHDR) since he is using `==`, not `===`. *edit* though I think you mean it so as to be able to compare with the leading zero. – ishegg Oct 07 '17 at 22:32
  • @ishegg That's true, but it doesn't hurt, either, and I think it makes it clearer what the code's trying to match. – Matt Gibson Oct 07 '17 at 22:33
1

Although @MattGibson already gave the right (well, it seems to be iMO) answser, I just wanna point out that there is a better way to achieve what it looks like you wanna achieve.

1st way:
Have an array with all the months

$months = [...,'08'=>"August", "09"=>"September",...];
// then 
$Month = $months[$IDKood[3] . $IDKood[4]];
// no need for all the elseifs

To make it better, genarate an int out of $IDKood[3] . $IDKood[4]:

$monthIndex = intval($IDKood[3] . $IDKood[4]);
// now you can use the built in dateTime-class to get the string for this month.
Jeff
  • 6,895
  • 1
  • 15
  • 33