2017-03-24 25 views
-1

我是圍繞做發揮從PHP5.3遷移生產代碼PHP7.1,我得到以下錯誤爲什麼在PHP7.1中2級別的中斷已被刪除,其替代解決方案可能是什麼?

PHP Fatal error: Cannot 'break' 2 levels

可能是什麼下面的代碼片段

$aud_found = false; 
    $audience = null; 
    foreach ($rules[0]['filterd_data_region'] as $k => $aud) { 
     if ($aud_found) 
      break; 

     $country = array(); 

     //country 
     if ($aud['area_type'] == 'country') { 
      foreach ($aud['selected_tag_data']['selected_content_tags'] as $arr){ 
       if ($aud_found) 
        break 2; 

       if ($ucountry == $arr['id']) { 
        $audience = $rules[0]['filterd_data_region'][$k]; 
        $audience_id = $k; 
        $aud_found = 'country'; 
       } 
      } 
     } 

     //region 
     if ($aud['area_type'] == 'region') { 
      foreach ($aud['selected_tag_data']['selected_content_tags'] as $arr){ 
       if ($aud_found) 
        break 2; 

       if ($uregion == $arr['id']) { 
        $audience = $rules[0]['filterd_data_region'][$k]; 
        $audience_id = $k; 
        $aud_found = 'region'; 
       } 
      } 
     } 

    } 
+1

我的回答對你有幫助嗎? – vanloc

+0

至少它運行但需要重新編寫邏輯。 –

回答

1

它的替代解決方案你可以解決這個問題有變化:

break 2; 

通過:

break; 

你進入一個循環而不是兩個嵌套循環。這就是爲什麼你不能「突破2」(因爲2表示你在嵌套循環內)。出現此錯誤是因爲PHP7比以前的版本更嚴格。

注意:您不能從if語句中「打破」。你只能從一個循環中突破。

相關問題