Georgi Georgiev
Georgi Georgiev

Reputation: 3964

PHP Fatal error: Cannot break/continue

if (isset($errors))  
{  
foreach ($errors as $error)  
  {  
    echo $error;  
  }    
}  
else {break 2;}  
// some more code

Outputs:

Fatal error: Cannot break/continue 2 levels  

I tried break 1, it didn't work either.

Upvotes: 6

Views: 12063

Answers (3)

Russell Dias
Russell Dias

Reputation: 73382

Break ends the execution within a foreach, for, while, do-while or switch structure..

if (isset($errors))  
{  
foreach ($errors as $error)  
  {  
    echo $error;  
  }    
}  
else {break 2;} //there is no loop here!  

Upvotes: 4

Phill Pafford
Phill Pafford

Reputation: 85368

if (isset($errors))  
{  
foreach ($errors as $error)  
  {  
    echo $error;  
  }    
}  

No need to use break as you seem to want to end on the else condition. just use the above code for your errors, it will be skipped if no errors. No need for break

Upvotes: 4

Guillaume Lebourgeois
Guillaume Lebourgeois

Reputation: 3873

Just type break not followed with any number. But break is helpless outside of a loop / block.

Upvotes: 0

Related Questions