Owen
Owen

Reputation: 7577

PHP switch() statement

Is this good practise ... ie, grouping the default case with another?

    switch ($cond){
            case 1:
                ...;
                break;
            case 2:
                ...;
                break;
            case 3:
            default:
                ...;
                break;
        }

Upvotes: 6

Views: 222

Answers (2)

Chuck Callebs
Chuck Callebs

Reputation: 16431

It makes perfect sense to do it that way.

Also, @Ian is correct, but in a limited scope. If you wanted additional functionality applied to case 3 you would leave it the way it is. As long as you don't break, it will go on to the next case.

Upvotes: 3

Ian Oxley
Ian Oxley

Reputation: 11056

It kind of makes case 3 redundant though, so I'd remove it and just leave it as default

Upvotes: 1

Related Questions