noochies Posted July 16, 2008 Share Posted July 16, 2008 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 Link to comment https://forums.phpfreaks.com/topic/115063-switch-statements-behavior-with-a-zero-value-comparison/ Share on other sites More sharing options...
teynon Posted July 16, 2008 Share Posted July 16, 2008 I'm kind of tired right now, but could it be because 0 in a variable is considered empty? Not exactly sure, does it work if you put case 0: ? Link to comment https://forums.phpfreaks.com/topic/115063-switch-statements-behavior-with-a-zero-value-comparison/#findComment-591728 Share on other sites More sharing options...
DyslexicDog Posted July 16, 2008 Share Posted July 16, 2008 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!'); } ?> Link to comment https://forums.phpfreaks.com/topic/115063-switch-statements-behavior-with-a-zero-value-comparison/#findComment-591729 Share on other sites More sharing options...
trq Posted July 16, 2008 Share Posted July 16, 2008 I'm not sure how to word it exactly but your case statements must match exactly that of your switch expression for that case to execute. Hence, in this case you would be better using an if elseif else. Link to comment https://forums.phpfreaks.com/topic/115063-switch-statements-behavior-with-a-zero-value-comparison/#findComment-591733 Share on other sites More sharing options...
kenrbnsn Posted July 16, 2008 Share Posted July 16, 2008 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 Link to comment https://forums.phpfreaks.com/topic/115063-switch-statements-behavior-with-a-zero-value-comparison/#findComment-591734 Share on other sites More sharing options...
trq Posted July 16, 2008 Share Posted July 16, 2008 Doh, yeah, that'll work. Hehe. Link to comment https://forums.phpfreaks.com/topic/115063-switch-statements-behavior-with-a-zero-value-comparison/#findComment-591735 Share on other sites More sharing options...
noochies Posted July 16, 2008 Author Share Posted July 16, 2008 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!) Link to comment https://forums.phpfreaks.com/topic/115063-switch-statements-behavior-with-a-zero-value-comparison/#findComment-591757 Share on other sites More sharing options...
kenrbnsn Posted July 16, 2008 Share Posted July 16, 2008 It worked for '75' because a zero value is also treated at "false" and anything other than 0 is treated as "true". Ken Link to comment https://forums.phpfreaks.com/topic/115063-switch-statements-behavior-with-a-zero-value-comparison/#findComment-591763 Share on other sites More sharing options...
noochies Posted July 16, 2008 Author Share Posted July 16, 2008 Makes total sense...thank you so much!!! Link to comment https://forums.phpfreaks.com/topic/115063-switch-statements-behavior-with-a-zero-value-comparison/#findComment-591771 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.