leke Posted June 27, 2012 Share Posted June 27, 2012 I was wondering if it was good practice to use switch without break for this example... <input type="checkbox" name="box1" value="1" /> <input type="checkbox" name="box2" value="1" /> <input type="checkbox" name="box3" value="1" /> <input type="checkbox" name="box4" value="1" /> $box1 = $_POST['box1']; $box2 = $_POST['box2']; $box3 = $_POST['box3']; $box4 = $_POST['box4']; switch ($i = '1') { case $box1: // do something; case $box2: // do something; case $box3: // do something; case $box4: // do something; default: echo "All checkboxes where left unchecked"; } ?> Or perhaps there would be a better way? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/264882-switch-without-break-question/ Share on other sites More sharing options...
xyph Posted June 27, 2012 Share Posted June 27, 2012 Test it. It will behave much differently than with breaks. Quote Link to comment https://forums.phpfreaks.com/topic/264882-switch-without-break-question/#findComment-1357438 Share on other sites More sharing options...
Pikachu2000 Posted June 27, 2012 Share Posted June 27, 2012 If I've gotten the right idea of what you're trying to do, I think it would be a better idea to set the checkboxes up as an array in the form, then foreach() through them when the form is submitted. Quote Link to comment https://forums.phpfreaks.com/topic/264882-switch-without-break-question/#findComment-1357440 Share on other sites More sharing options...
Barand Posted June 27, 2012 Share Posted June 27, 2012 There will be times, like this example where, say, if the value is 1 then do actions 1 and 2. As a standard, I would recommend putting a "fallthru" comment where this is happening so it clear to anyone later maintaining the code that this is the intended flow and not a missing "break". switch ($x) { case 1: echo "Action 1<br />"; // fallthru case 2: echo "Action 2<br />"; break; case 3: echo "Action 3<br />"; break; } Quote Link to comment https://forums.phpfreaks.com/topic/264882-switch-without-break-question/#findComment-1357452 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.