PHP错误:未加括号`a?b:c?d:e`已过时.使用`(a?b:c)?d:e`或`a?b:(c?d:e)` [英] PHP Error : Unparenthesized `a ? b : c ? d : e` is deprecated. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`

查看:833
本文介绍了PHP错误:未加括号`a?b:c?d:e`已过时.使用`(a?b:c)?d:e`或`a?b:(c?d:e)`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 PHP 7.4用于laravel应用程序,并且我经常遇到此异常.

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)`

触发此异常的代码是:

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?

发现由于PHP的某些升级,这是E_DEPRECATED错误,但是有什么方法可以通过将不赞成使用的代码转换为最新版本来解决此异常?

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?

推荐答案

在php中所做的此更改是为了消除决策树中的歧义,以便有明确的条件执行顺序.

This change in php has been done to remove ambiguity in the decision tree so that there is an explicit order of condition execution.

此处已弃用警告:

代码:

$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);

输出:

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',
)

作为脚本的人工阅读器,我假设您的状况从左到右读取-但这会将 Spanish1 作为输出值.

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.

即使在php7.4之前,输出也是 Spanish2 ,因为在决策树中后者的优先级被赋予了优先级.

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.

此外,我也同意@Laurel的观点,在php7中,是时候让您接受语法合并的甜味了,它是null合并运算符.这样可以避免优先级问题和使用括号的需要,但是根据所需的结果,您可能需要重新排列条件.

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.

$ values 的优先级:( 演示)

$original[$languageKey] =
    $values[$languageKey]
        ?? $filesContent[$fileName][$languageKey][$key]
            ?? '';

$ filesContent 的优先级:( 演示)

$original[$languageKey] =
    $filesContent[$fileName][$languageKey][$key]
        ?? $values[$languageKey]
            ?? '';

P.s.IIRC,php手册基于代码的清晰性,建议不要使用像这样的嵌套三元/条件.我不介意这种情况,并且我喜欢避免代码膨胀,但是其他开发人员可能会采取更为纯粹的立场.

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.

这篇关于PHP错误:未加括号`a?b:c?d:e`已过时.使用`(a?b:c)?d:e`或`a?b:(c?d:e)`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆