Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP switch() statement

Tags:

php

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

    switch ($cond){
            case 1:
                ...;
                break;
            case 2:
                ...;
                break;
            case 3:
            default:
                ...;
                break;
        }
like image 831
Owen Avatar asked Sep 12 '11 16:09

Owen


2 Answers

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.

like image 171
Chuck Callebs Avatar answered Oct 23 '22 17:10

Chuck Callebs


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

like image 39
Ian Oxley Avatar answered Oct 23 '22 15:10

Ian Oxley