31

How can I exit a if block if a certain condition is met?

I tried using break but it doesn't work:

if($bla): 
  $bla = get_bla();
  if(empty($bla)) break;
  do($bla);
endif;

it says: Fatal error: Cannot break/continue 1 level in...

Alex
  • 66,732
  • 177
  • 439
  • 641

12 Answers12

46

In PHP 5.3 you can use goto

if($bla): 
   $bla = get_bla();
   if(empty($bla)) goto end;
   do($bla);
endif;
end:

But personally I think that's an ugly solution.

Burak Guzel
  • 1,267
  • 1
  • 12
  • 12
  • 7
    Why ugly? It would be useful in certain situations for sure. Vote up! – doc_id Mar 05 '11 at 13:57
  • 1
    May I ask why the people behind PHP introduced it in 2009? They could have chosen to not introduce it at all. I agree that it feels very DOS-like to me (not in the good way), but it can come in handy for sure. – tschoffelen Mar 20 '13 at 08:51
  • It still has a purpose, exiting out of nested loops for example. – Petter Pettersson Mar 24 '21 at 14:37
15

You can't break if statements, only loops like for or while.

If this if is in a function, use 'return'.

Oli
  • 2,370
  • 2
  • 26
  • 42
13

Why not just turn it around.

if($bla): 
  $bla = get_bla();
  if(!empty($bla)) {
    do($bla);
  }
endif;

That way it will only run your code if $bla isn't empty.. That's kinda the point with if-statements

Sondre
  • 1,878
  • 18
  • 33
  • 32
    Your answer solves the problem but doesn't answer the question "How can I exit an if block", it worth to note if it's ever possible. – doc_id Mar 05 '11 at 13:55
  • 2
    @rahmanisback Like you say, it solves the problem so I don't really see what's wrong with my answer. If you just look one answer down you'll see that the question is answered. When I posted my answer there were already several posts answering this, so what I did was provide an actual solution. You can't break an if statement and if you ever need to do so you're using it wrong. – Sondre Mar 07 '11 at 09:37
  • It solved my problem! I was doing the same thing, trying to exit when I could rearrange. I see the merit in actually answering the question, but if the question is using an improper method or not practical, it should be noted. Thanks! – Jacob Raccuia Apr 24 '13 at 18:59
5

I cant believe no one have post this solution yet (writing it in my PHP style):

if($bla){do{
  $bla = get_bla();
  if(empty($bla)) break;
  do($bla);
}while(false);}

Complexity still O(1)

  • 2
    Wow. But it's still totally perverted... We have goto at our disposal, we can even use an include() and return from there, but we can't just quit the if block... – Marki Mar 27 '16 at 21:08
4

Try this

do {

    if($bla) {
        $bla = get_bla();

        if (empty($bla)) {
            break;
        }

        do($bla);
    }

    /* You can do more comparisions here */

} while (0);
Greeso
  • 7,544
  • 9
  • 51
  • 77
2

For me it helps to have an escape marker in case the code needs to exit between blocks if you don't mind having an if statement in another.

$exit = FALSE;

if(!$exit){
    if($data["param1"] == cond1){
        //do something and continue
    }
    else{
        //do something
        $exit = TRUE;
    }
}

if(!$exit){
    if($data["param2"] == cond2){
        //do something and continue
    }
    else{
        //do something
        $exit = TRUE;
    }
}

{...}

If you keep placing conditional statements around each block, it will not execute any other blocks after you set $exit to true. You can name the variable $continue and revert its roles if that makes more sense to you.

it works easier if you have no else statements.

$exit = FALSE;

if($bla): 
    $bla = get_bla();
    if(empty($bla)) $exit = TRUE;
    if(!$exit)do($bla);
endif;
2

You can't.

You could put the whole thing into a function from which you can return.

Otherwise, you'll have to change the logic and include another if block.

if($bla): 
  $bla = get_bla();
  if(!empty($bla)): 
   do($bla);
  endif;
endif;
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
1

Strictly speaking, we can't leave if-block prematurely, but sometimes it's really needed. break can be used only inside loops and switch-block. But we can place if in a loop. So, answer of Marcos Fernandez Ramos is probably the most appropriate. Here is just slightly nicer variant.

do if ()
{
  ...
  if () break;
  ...
} while (false);
1

If you really want a statement from which you can break, you could use a while statement:

while ($bla) {
    $bla = get_bla();

    if (!$bla) {
        break;
    }

    do($bla);

    // Maybe some more code...

    // Do not forget to break out of the while loop!
    break;
}

This is a good way of avoiding many nested if statements (which can maybe be avoided in many cases), but do be careful to break the loop.

Marian
  • 1,154
  • 2
  • 15
  • 25
0

if your condition is a test against a single variable, then the switch statement is a much better solution and you can easily break out:

switch ($var_to_check) { // equivalent to if ($var_to_check==$value_against)
    case $value_against:
        ...
        if ($something_wrong) { break; } // early exit from if
        ...
        break; // regular break
    default: // equivalent to else
        ...
} // end of switch (or if)
... // after the tests

It only works for single test checks. Has the advantage of handling a bunch of else ifs.

Jacques Amar
  • 1,803
  • 1
  • 10
  • 12
0

Just replace IF with WHILE and add break; at the end of the new while statement and anywhere in middle too.

Harijs Krūtainis
  • 1,261
  • 14
  • 13
0

You can create a function for the contains of your IF block. For example

if($bla): 
  doSomething();
endif;

Then you can simply use RETURN

function doSomething() {
    $bla = get_bla();
    if(empty($bla)) return;
    do($bla);
}

It's looking more clearly than multiply IF statements