Reputation:
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
.
Why is that?
Why did it work for 907
and not for 909
, even though both have the same data type?
I know they are strings and I should compare string with string, but why is it working with one example and not with another?
Upvotes: 2
Views: 71
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
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
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
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