I have a really dumb question and not really important but it has been bugging me for quite some time: how do I indent "correctly" the "break;" inside a switch statement in PHP?
Like this:
case "foo":
do_whatever(TRUE);
break;
case "bar":
...
or like this?
case "foo":
do_whatever(TRUE);
break;
case "bar":
...
As I have said, it is silly but I'd like to know from more experienced programmers so that I can stick with one style (the most common one) and don't get baffled everytime.
Thank you!
That depends on which standard you're following. PSR-2 indents break:
<?php
switch ($expr) {
case 0:
echo 'First case, with a break';
break;
case 1:
echo 'Second case, which falls through';
// no break
case 2:
case 3:
case 4:
echo 'Third case, return instead of break';
return;
default:
echo 'Default case';
break;
}
I believe this style is more common than the other.
Technically the break;
is a statement like the echo
, so it should be on the same level.
Also, omitting the break;
is allowed, case
and break;
don't really form a pair, the break;
may be omitted for special effect (fall through).