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...
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...
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
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'.
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)
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);
Try this
do {
if($bla) {
$bla = get_bla();
if (empty($bla)) {
break;
}
do($bla);
}
/* You can do more comparisions here */
} while (0);
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.