user1642018
user1642018

Reputation:

Strange issue comparing numbers inside switch

I am trying to generate the css class name from the category id using switch case like this.

I have mutiple conditions in switch case, but we will look only this one as its creating strange output.

Sample code:

<?php
$value = '907';//base value

$value_array =  str_split($value);//create array of string, if its int.

var_dump($value_array);//debug whats in array

switch($value_array[0]){

case 9:

$final = 'i came inside 9';

if($value_array[1].$value_array[2] == 07){
//check whther last 2 digits are 07
    $final = 'i came inside 907';
}else if($value_array[1].$value_array[2] == 09){
//chcek whether last 2 digits are 09
    $final = 'i came inside 909';
}
break;
}

echo $final;

The above code gives output as [$value is 907]:

array(3) {
  [0]=>
  string(1) "9"
  [1]=>
  string(1) "0"
  [2]=>
  string(1) "7"
}
i came inside 907

Which is the correct behavior. But if I change the base value from 907 to 909, then output comes as [$value is 909].

array(3) {
  [0]=>
  string(1) "9"
  [1]=>
  string(1) "0"
  [2]=>
  string(1) "9"
}
i came inside 9

The output should be i came inside 909.

Upvotes: 2

Views: 71

Answers (4)

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21422

Because in php 09 will consider it as octal number and convert it into 0 where as for 07 its always 07

When you try to echo 09 it'll output you 0 and for 07 its 07

So instead of comparing it loosely == you need to use strict comparison === i.e

if($value_array[1].$value_array[2] === "07"){
//check whther last 2 digits are 07
    $final = 'i came inside 907';
}else if($value_array[1].$value_array[2] === "09"){
//chcek whether last 2 digits are 09
    $final = 'i came inside 909';
}

Upvotes: 0

davidgiga1993
davidgiga1993

Reputation: 2853

You're comparing the array value with an integer formatted as octal number (see http://php.net/manual/de/language.types.integer.php).

07 is a valid octal number an represents the value 7 and your comparison works.

09 on the other hand is an invalid octal number. Therefore the comparison doesn't work.

In order to fix your issue you need to put ' around the values so they're interpreted as strings.

if($value_array[1].$value_array[2] == '07'){
//check whther last 2 digits are 07
    $final = 'i came inside 907';
}else if($value_array[1].$value_array[2] == '09'){
//chcek whether last 2 digits are 09
    $final = 'i came inside 909';
}

Upvotes: 2

Will
Will

Reputation: 24699

When you use 07, PHP interprets it as an octal number. It knows that 09 is not octal, because 9 is not valid in the Octal system.

Try 7 and 9, or '07' and '09'.

<?php
$value = '907'; //base value

$value_array =  str_split($value); //create array of string, if its int.

var_dump($value_array); //debug whats in array

switch ($value_array[0])
{
    case 9:
        $final = 'i came inside 9';

        if ($value_array[1].$value_array[2] == '07')
        {
            //check whther last 2 digits are 07
            $final = 'i came inside 907';
        }
        elseif($value_array[1].$value_array[2] == '09')
        {
            //chcek whether last 2 digits are 09
            $final = 'i came inside 909';
        }

        break;
}

echo $final;

Upvotes: 0

Rizier123
Rizier123

Reputation: 59691

07 and 09 are octal numbers, where 09 is an invalid octal number, so it will end up as 0. That's why your code doesn't work as you want it to.

To solve it just put it in quotes, e.g.

if($value_array[1].$value_array[2] === "07"){
//check whther last 2 digits are 07
    $final = 'i came inside 907';
}else if($value_array[1].$value_array[2] === "09"){
//chcek whether last 2 digits are 09
    $final = 'i came inside 909';
}

Upvotes: 4

Related Questions