EastsideDev
EastsideDev

Reputation: 6639

Switch statement and NULL versus 0

switch ($i) {
    case NULL:
        echo "It is NULL";
        break;
    case 0:
        echo "It is zero";
        break;
}

If I set $i to NULl or 0, the switch statement is evaluating it to NULL. Is switch not ready to handle the equivalent of this:

if ($i === NULL) {
    echo 'This is NULL';
}
if ($i == 0) {
    echo 'This is Zero';
}

if so, should I assume I have to have an IF loop instead of SWITCH?

Upvotes: 1

Views: 1540

Answers (1)

Robbie Smith
Robbie Smith

Reputation: 482

I was also going to suggest casting your switch input variable as something you can expect like (int) or (string) to make sure you're validating the cases properly.

Upvotes: 2

Related Questions