問題描述
我正在將 PHP 7.4 用于 Laravel 應(yīng)用程序,并且我經(jīng)常收到此異常.
ErrorException (E_DEPRECATED)無括號的`a ?乙:丙?d : e` 已棄用.使用`(a ? b : c) ?d : e` 還是 `a ?b : (c ? d : e)`
觸發(fā)這個異常的代碼是:
foreach ($allLanguages as $languageKey) {$original[$languageKey] =isset($values[$languageKey])?$values[$languageKey]: isset($filesContent[$fileName][$languageKey][$key]) ?$filesContent[$fileName][$languageKey][$key] : '';}
有人能幫我解決這個問題嗎?
發(fā)現(xiàn)這是由于 PHP 中的某些升級導(dǎo)致的 E_DEPRECATED 錯誤,但是有什么方法可以通過將不推薦使用的代碼轉(zhuǎn)換為最新代碼來解決此異常?
在 php 中進(jìn)行了此更改以消除決策樹中的歧義,以便有明確的條件執(zhí)行順序.
棄用警告在此處轉(zhuǎn)載:
代碼:
$allLanguages = ['en', 'es', 'fr'];$values = ['es' =>'西班牙語1'];$文件內(nèi)容 = ['富' =>['es' =>['酒吧' =>'西班牙語2'],'fr' =>['酒吧' =>'法語']]];$文件名 = 'foo';$key = 'bar';$原始 = [];foreach ($allLanguages as $languageKey) {$original[$languageKey] =isset($values[$languageKey])?$values[$languageKey]: isset($filesContent[$fileName][$languageKey][$key])?$filesContent[$fileName][$languageKey][$key]: '';}var_export($original);
輸出:
<塊引用>已棄用:未加括號的 `a ?乙:丙?d : e` 已棄用.使用`(a ? b : c) ?d : e` 還是 `a ?b : (c ? d : e)` 在第 17 行的/in/TG4g2
數(shù)組 ('en' =>'','es' =>'西班牙語2','fr' =>'法語',)
作為腳本的人類讀者,我會假設(shè)從左到右讀取您的條件——但這會將 Spanish1
作為輸出值.
即使在 php7.4 之前,輸出也是Spanish2
,因為后者在決策樹中的分叉被優(yōu)先考慮.
為避免這種情況,您必須將條件括在括號中,以準(zhǔn)確說明應(yīng)如何處理執(zhí)行順序.
另外,我同意@Laurel 的觀點(diǎn),在 php7 中是時候讓您接受空合并運(yùn)算符的句法甜蜜了.這將避免優(yōu)先級問題和使用括號的需要,但根據(jù)您想要的結(jié)果,您可能需要對條件重新排序.
優(yōu)先于$values
:(Demo)
$original[$languageKey] =$values[$languageKey]??$filesContent[$fileName][$languageKey][$key]??'';
優(yōu)先于$filesContent
:(Demo)
$original[$languageKey] =$filesContent[$fileName][$languageKey][$key]??$values[$languageKey]??'';
附言IIRC,php 手冊建議不要在代碼清晰的基礎(chǔ)上使用這樣的嵌套三元組/條件.我不介意這種情況,我喜歡避免代碼膨脹,但其他開發(fā)人員可能會采取更純粹的立場.
I am using PHP 7.4 for a laravel application and I am getting this exception very frequently.
ErrorException (E_DEPRECATED)
Unparenthesized `a ? b : c ? d : e` is deprecated. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`
The code which triggers this exception is :
foreach ($allLanguages as $languageKey) {
$original[$languageKey] =
isset($values[$languageKey])
? $values[$languageKey]
: isset($filesContent[$fileName][$languageKey][$key]) ? $filesContent[$fileName][$languageKey][$key] : '';
}
Can any help me to resolve this issue?
Found that this is E_DEPRECATED error due to some upgrade in PHP, but is there any way to resolve this exception by converting the deprecated code to latest?
This change in php has been done to remove ambiguity in the decision tree so that there is an explicit order of condition execution.
The deprecation warning is reproduced here:
Code:
$allLanguages = ['en', 'es', 'fr'];
$values = ['es' => 'Spanish1'];
$filesContent = [
'foo' => [
'es' => ['bar' => 'Spanish2'],
'fr' => ['bar' => 'French']
]
];
$fileName = 'foo';
$key = 'bar';
$original = [];
foreach ($allLanguages as $languageKey) {
$original[$languageKey] =
isset($values[$languageKey])
? $values[$languageKey]
: isset($filesContent[$fileName][$languageKey][$key])
? $filesContent[$fileName][$languageKey][$key]
: '';
}
var_export($original);
Output:
Deprecated: Unparenthesized `a ? b : c ? d : e` is deprecated. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)` in /in/TG4g2 on line 17
array (
'en' => '',
'es' => 'Spanish2',
'fr' => 'French',
)
As a human-reader of your script, I would assume the reading of your condition as left to right -- but this would place Spanish1
as the output value.
Even before php7.4, the output isSpanish2
because the latter fork in the decision tree is given precedence.
To avoid this, you must wrap your conditions in parentheses to dictate exactly how the order of execution should be handled.
Also, I agree with @Laurel that in php7 it is time for you to embrace the syntactic sugary sweetness that is the null coalescing operator. This will avoid precedence issues and the need to use parentheses, but depending on your desired results, you may need to reorder your conditions.
Priority to $values
: (Demo)
$original[$languageKey] =
$values[$languageKey]
?? $filesContent[$fileName][$languageKey][$key]
?? '';
Priority to $filesContent
: (Demo)
$original[$languageKey] =
$filesContent[$fileName][$languageKey][$key]
?? $values[$languageKey]
?? '';
P.s. IIRC, the php manual advises against the use of nested ternaries/conditionals like this on the basis of code clarity. I don't mind this scenario and I like the avoidance of code bloat, but other devs may take a more purist stance.
這篇關(guān)于PHP 錯誤:未加括號的 `a ?乙:丙?d : e` 已棄用.使用`(a ? b : c) ?d : e` 還是 `a ?b : (c ? d : e)`的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!