PHP - exit from IF block

Alex picture Alex · Dec 28, 2010 · Viewed 80k times · Source

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

Answer

Burak Guzel picture Burak Guzel · Dec 28, 2010

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.