soycharliente Posted December 22, 2008 Share Posted December 22, 2008 I see multiple examples online and in the manual about the use of break in a switch. Some use a break statement in the default case and some don't. I was just curious if it mattered at all. Any insights? Quote Link to comment https://forums.phpfreaks.com/topic/138042-solved-switch-deafult-case-break/ Share on other sites More sharing options...
dennismonsewicz Posted December 22, 2008 Share Posted December 22, 2008 yeah if you don't use the break the next switch case will evaluate along with the previous one. You can stack cases on top of each other. example (stacking cases): switch($action) { case "test1": case "test2": //code here break; } Quote Link to comment https://forums.phpfreaks.com/topic/138042-solved-switch-deafult-case-break/#findComment-721494 Share on other sites More sharing options...
soycharliente Posted December 22, 2008 Author Share Posted December 22, 2008 As per my original post, I am specifically talking about the default case. Quote Link to comment https://forums.phpfreaks.com/topic/138042-solved-switch-deafult-case-break/#findComment-721498 Share on other sites More sharing options...
wildteen88 Posted December 22, 2008 Share Posted December 22, 2008 You don't have to specify a default case no, you can leave this out if you so wish to. The default case will be run if none of the cases matched the argument. Quote Link to comment https://forums.phpfreaks.com/topic/138042-solved-switch-deafult-case-break/#findComment-721518 Share on other sites More sharing options...
premiso Posted December 22, 2008 Share Posted December 22, 2008 As far as the break, no you do not have to use it. The break is under each case where you want it to stop executing without it it just runs. But since the default should be your last case or part of a case it is not necessary. A few examples: <?php $page = 'index.html'; switch ($page) { case 'blue.html': //this will execute if blue or yellow. case 'yellow.html': break; default: // this will execute if no data is passed in or index.html case 'index.html': break; } That is a valid switch statement, since the default is associated with the index it will run; <?php $page = 'index.html'; switch ($page) { case 'index.html': break; case 'yellow.html': break; default: } Is also valid incase you wanted a special circumstance for the no page <?php $page = 'index.html'; switch ($page) { case 'index.html': break; case 'blue.html': // extra coding if it is blue case 'yellow.html': // coding if yellow break; default: break; } Is also valid but the last break is unnecessary. That is valid with the blue and yellow so if they both have the primary principles but you want to do something different if the case is blue as appose to yellow it will allow you to. Anyhow hope that helps ya out. Quote Link to comment https://forums.phpfreaks.com/topic/138042-solved-switch-deafult-case-break/#findComment-721543 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.