4

I have an idiot question but I'm stuck.

here is my simply code

$my_string = '00';

switch ((string)$my_string)
{
    case '-1': $return_string = 'bla bla..'; break;
    case '0': $return_string = 'One zero'; break;
    case '00': $return_string = 'Double zero'; break;
    default: $return_string = 'default'; break;
}

echo $return_string;

The result of the code before returns

One Zero

any suggestions ?

Nanne
  • 64,065
  • 16
  • 119
  • 163
AkisC
  • 817
  • 2
  • 12
  • 27
  • 1
    This is very common source of errors in PHP. What might be even more surprising is the fact that `("0xFF"==255) => True` – jedwards Jun 09 '12 at 07:43
  • 1
    "If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value." - [PHP Spec](http://php.net/manual/en/language.operators.comparison.php) – jedwards Jun 09 '12 at 07:56

4 Answers4

4

switch uses equality, not identity. Since '0' equals '00' (in that they both evaluate to 0), the structure won't work.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Is there eny way to check that ? I have try both switch and if statements but the result its the same – AkisC Jun 09 '12 at 07:32
2

Well, this is basically because php is not a strongly type language. so "00" == "0" gives true (and as said switch uses the equality operator)

you might change this to if else statements with triple equals and that would do it.

"00" === "0" gives false. check this thread for more details. How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?

Community
  • 1
  • 1
avk
  • 994
  • 6
  • 13
1

As pointed out above: switch doesn't check for identical type and value (difference between == and ===). To get a switch working in your case, you could do this:

$my_string = '00';
switch (true)
{
    case ((string) $my_string === '-1'): $return_string = 'bla bla..'; break;
    case ((string) $my_string === '0'): $return_string = 'One zero'; break;
    case ((string) $my_string === '00'): $return_string = 'Double zero'; break;
    default: $return_string = 'default'; break;
}
Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • This is rather obtuse and it'd probably be more readable simply to use `if` clauses instead. – Will Vousden Jun 09 '12 at 08:35
  • as far as readability is concerned, I agree. But since I don't know what the OP is actually trying to do, I wouldn't go as far as to suggest `if` branching, since `switch` has the benefit of fall-through (I consider it to be a benefit, at least) and, when dealing with 10+ cases, it dramatically improves readability, IMO. Even in this case. I must admit, it would be more readible to convert `$my_string` to a string beforehand. – Elias Van Ootegem Jun 09 '12 at 13:48
0

Another solution is:

switch ('_'.$my_string)
{
    case '_-1': $return_string = 'bla bla..'  ; break;
    case '_0' : $return_string = 'One zero'   ; break;
    case '_00': $return_string = 'Double zero'; break;
    default   : $return_string = 'default'    ; break;
}
Jacob Smith
  • 89
  • 1
  • 5