views:

144

answers:

6

Is there anyway to make it so that the following code still uses a switch and returns b not a? Thanks!

$var = 0;
switch($var) {
    case NULL : return 'a'; break;
    default : return 'b'; break;
}

Using if statements, of course, you'd do it like this:

$var = 0;
if($var === NULL) return 'a';
else return 'b';

But for more complex examples, this becomes verbose.

+2  A: 

Sorry, you cannot use a === comparison in switch, since according to the switch() documentaion:

Note that switch/case does loose comparision.

This means you'll have to come up with a work around. From the loose comparisons table, you could make use of the fact that NULL == "0" is false by type casting:

<?php
$var = 0;
switch((string)$var) 
{
    case "" : echo 'a'; break; // This tests for NULL or empty string   
    default : echo 'b'; break; // Everything else, including zero
}
// Output: 'b'
?>

Live Demo

Peter Ajtai
Then what's the point of `switch`? Fall-through?
strager
Well, there are other circumstances (not just with NULLs) that I'd like this to work with.
aharon
This will not work! This is equivalent to `if ($var == is_null($var))` which will never be true (if `$var` is `NULL`, it'll be `NULL == true`).
Daniel Vandersluis
@Daniel - Thanks. Looks like it can't be done without some sort of work around :(
Peter Ajtai
A: 

make switch use === comparison not == comparison In PHP

Unfortunately switch uses loose comparison and as far as I know there's no way to change that.

quantumSoup
+1  A: 

Nope. From the manual page:

Note that switch/case does loose comparision.

If you only have two conditions, use an if like your second example. Otherwise, check for NULL first and switch on the other possibilities:

if (is_null($var))
{
  return 'a';
}

switch ($var)
{
    // ...
}
Daniel Vandersluis
+1  A: 

This is not possible.

You can, however, put the if statements inside the switch:

switch($var) {
    // Loose cases here

    case 0:
        if($var === NULL) {
            return 'a';
        }

        // Fall through

    default:
        return 'b';
}

Or simply:

switch($var) {
    // Loose cases here

    default:
        if($var === NULL) {
            return 'a';
        }

        return 'b';
}
strager
At that point why bother with the switch? You could even do `switch (true) { case $var === null: ... }
ircmaxell
@ircmaxell, I assumed the OP wants fall-through logic or otherwise a reason to use `switch` beyond strict checking at times.
strager
+7  A: 

One way you could do this is to do what I think of as a backwards switch (there may be a more official name for it). The idea is that you put true or false at the top and then your conditionals in the case statements.

switch(true)
{
  case ($var === NULL):
    return 'a';
    break;
  case ($var === 0):
    return 'b';
    break;
  default:
    return 'c';
}
Greg W
what's the point of this vs just an if/elseif block?
aharon
There's no point of doing this, and if you want to check against any other values the syntax will get pretty awkward.
Daniel Vandersluis
Nothing really other than coding style, the end result will be the same. I think switches look a little tidier if you have a large number of conditions. I'd rather look at 10 cases than if/elseif/elseif/elseif etc.
Greg W
On second consideration, I'm liking this.
Peter Ajtai
+1  A: 

You can also switch on the type of the variable:

switch (gettype($var)) {
...
}
greg
Bad idea - From the docs: `Never use gettype() to test for a certain type, since the returned string may be subject to change in a future version. In addition, it is slow too, as it involves string comparison. Instead, use the is_* functions.`
Peter Ajtai