atholon Posted February 20, 2008 Share Posted February 20, 2008 If you were to have a switch statement like this: switch ($number) { case '1': case '2': $var = "hamburger"; break; case '3'; $var = "french fry"; break; default: $var = "milk shake"; break; } How would the first case execute? Would it be something like this? if ($number= '1') { } if ($number= '2') { $var = "hamburger"; } else if ($number= '3') { $var = "french fry"; } else { $var = "milk shake"; } Link to comment https://forums.phpfreaks.com/topic/92092-switch-statements/ Share on other sites More sharing options...
rhodesa Posted February 20, 2008 Share Posted February 20, 2008 Switch statements work similar to a waterfall. It will test each case going down until it finds a match, then will continue to run every subsequent case until it reaches a break. So, if $number equals 1, it would still set $var = "hamburger". So think of it more like: if ($number= '1' || $number = '2') } $var = "hamburger"; } else if ($number= '3') { $var = "french fry"; } else { $var = "milk shake"; } Link to comment https://forums.phpfreaks.com/topic/92092-switch-statements/#findComment-471587 Share on other sites More sharing options...
atholon Posted February 20, 2008 Author Share Posted February 20, 2008 ohh I thought that it would still test the second case '2': even if case one was true before it changed the var's value. Anywho thanks! Link to comment https://forums.phpfreaks.com/topic/92092-switch-statements/#findComment-471693 Share on other sites More sharing options...
Isityou Posted February 20, 2008 Share Posted February 20, 2008 Nope, once it hits a break it will exit the switch and all subsequent code will not be executed. Link to comment https://forums.phpfreaks.com/topic/92092-switch-statements/#findComment-471700 Share on other sites More sharing options...
atholon Posted February 20, 2008 Author Share Posted February 20, 2008 right but there is no break between then case '1' and case '2' so I assumed that it would just test case one, then test case 2 and if case 2 were true that it would assign the value. It's nice to know that it is more like an "or" Link to comment https://forums.phpfreaks.com/topic/92092-switch-statements/#findComment-471785 Share on other sites More sharing options...
rhodesa Posted February 20, 2008 Share Posted February 20, 2008 But keep in mind, something like this: <?php $food = array(); switch ($number) { case '1': $food[] = "hamburger"; case '2': $food[] = "potato chips"; break; case '3'; $food[] = "french fries"; break; default: $var = "milk shake"; break; } echo "I would like: ".implode(',',$food); ?> will produce these results (note case 1): CASERESULT 1I would like: hamburger,potato chips 2I would like: potato chips 3I would like: french fries 4I would like: milk shake Link to comment https://forums.phpfreaks.com/topic/92092-switch-statements/#findComment-471830 Share on other sites More sharing options...
atholon Posted February 20, 2008 Author Share Posted February 20, 2008 awesome. thanks! Link to comment https://forums.phpfreaks.com/topic/92092-switch-statements/#findComment-471838 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.