Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch/Break indenting in PHP [closed]

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!

like image 540
Federico Pirani Avatar asked Oct 24 '25 06:10

Federico Pirani


2 Answers

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.

like image 129
Jamie Schembri Avatar answered Oct 26 '25 19:10

Jamie Schembri


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).

like image 45
Adder Avatar answered Oct 26 '25 21:10

Adder