Jump to content

Switch statement's behavior with a zero value comparison


noochies

Recommended Posts

Hello,

 

I am having a hard time understanding what a switch statement I have is doing. I am running php version 4.4.7.

 

When zero is the value passed to the switch statement and it tries to compare zero to another number (in this case 10), execution will fall to the default case statement, even though clearly, zero is less than 10. Can someone explain to me what's going on?

 

(Obviously, this is simplified code)

 

//set stock price to zero
$stock_price = 0;

//make sure it's an int
settype($stock_price, "integer");

switch($stock_price) {
    case $stock_price<10:
        print("stock price is less than 10");
        break;
    case $stock_price<100:
        print("stock price is less than 100");
break;
    case $stock_price<1000:
        print("stock price is less than 1000");
break;
    default:
print('ERROR!');
}

 

This will print 'ERROR!', instead of 'stock price is less than 10' like I would expect.

I'm stumped

Try This

 

<?php
//set stock price to zero
$stock_price = 0;

//make sure it's an int
settype($stock_price, "integer");

switch($stock_price) {
   case ($stock_price<10):
       print("stock price is less than 10");
       break;
   case ($stock_price<100):
       print("stock price is less than 100");
break;
   case ($stock_price<1000):
       print("stock price is less than 1000");
break;
   default:
print('ERROR!');
}
?>

To do this type of switch, you have to switch on "true":

<?php
//set stock price to zero
$stock_price = 0;

//make sure it's an int
// settype($stock_price, "integer");  <-- this is not needed

switch(true) {
    case $stock_price<10:
        print("stock price is less than 10");
        break;
    case $stock_price<100:
        print("stock price is less than 100");
break;
    case $stock_price<1000:
        print("stock price is less than 1000");
break;
    default:
print('ERROR!');
}?>

 

Ken

Yeah, I tried parensthesis (didn't help) and the 'case: 0' statement DOES work.

 

So kenrbnsn, I think I see what you are saying, it makes total sense but didn't occur to me.

 

But here's a follow up question. Why did it work for all numbers >= 1?

For example, if 75 was passed into the switch statement, following your logic, it would try to compare 75 to 1 in the ($stock_price < 100) case and it would go to the default case because 75 != 1. But it doesn't. Is that because it is coercing the 75 to a boolean (since it's trying to compare it to a boolean) which would turn it into a 1 and so then the comparison succeeds because 1 == 1?

 

(By the way I am SUPER impressed with this forum. I can't believe how quickly I got responses, this is great!)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.