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
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.