为什么在PHP7.1中2级别的中断已被删除,其替代解决方案可能是什么?
问题描述:
我是围绕做发挥从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';
}
}
}
}
答
它的替代解决方案你可以解决这个问题有变化:
break 2;
通过:
break;
你进入一个循环而不是两个嵌套循环。这就是为什么你不能“突破2”(因为2表示你在嵌套循环内)。出现此错误是因为PHP7比以前的版本更严格。
注意:您不能从if语句中“打破”。你只能从一个循环中突破。
我的回答对你有帮助吗? – vanloc
至少它运行但需要重新编写逻辑。 –