开发者

PHP - exit from IF block

How can I exit a if block if a c开发者_C百科ertain 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...


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.


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

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


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


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)


Try this

do {

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

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

        do($bla);
    }

    /* You can do more comparisions here */

} while (0);


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;


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;


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


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.


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜