Jump to content

Switch without break question


leke

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/264882-switch-without-break-question/
Share on other sites

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.

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;
}

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.