PHP Fatal error: Cannot break/continue

不羁岁月 提交于 2019-12-12 10:47:47

问题


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.


回答1:


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




回答2:


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!  



回答3:


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



来源:https://stackoverflow.com/questions/3618030/php-fatal-error-cannot-break-continue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!