• <bdo id='l3Iu5'></bdo><ul id='l3Iu5'></ul>

    <small id='l3Iu5'></small><noframes id='l3Iu5'>

      1. <legend id='l3Iu5'><style id='l3Iu5'><dir id='l3Iu5'><q id='l3Iu5'></q></dir></style></legend>
      2. <tfoot id='l3Iu5'></tfoot>

        <i id='l3Iu5'><tr id='l3Iu5'><dt id='l3Iu5'><q id='l3Iu5'><span id='l3Iu5'><b id='l3Iu5'><form id='l3Iu5'><ins id='l3Iu5'></ins><ul id='l3Iu5'></ul><sub id='l3Iu5'></sub></form><legend id='l3Iu5'></legend><bdo id='l3Iu5'><pre id='l3Iu5'><center id='l3Iu5'></center></pre></bdo></b><th id='l3Iu5'></th></span></q></dt></tr></i><div id='l3Iu5'><tfoot id='l3Iu5'></tfoot><dl id='l3Iu5'><fieldset id='l3Iu5'></fieldset></dl></div>
      3. PHP 错误:未加括号的 `a ?乙:丙?d : e` 已弃用.使用

        时间:2023-09-23
          <tfoot id='PsyoK'></tfoot>

            <small id='PsyoK'></small><noframes id='PsyoK'>

            <legend id='PsyoK'><style id='PsyoK'><dir id='PsyoK'><q id='PsyoK'></q></dir></style></legend>
              <tbody id='PsyoK'></tbody>
                <bdo id='PsyoK'></bdo><ul id='PsyoK'></ul>
              • <i id='PsyoK'><tr id='PsyoK'><dt id='PsyoK'><q id='PsyoK'><span id='PsyoK'><b id='PsyoK'><form id='PsyoK'><ins id='PsyoK'></ins><ul id='PsyoK'></ul><sub id='PsyoK'></sub></form><legend id='PsyoK'></legend><bdo id='PsyoK'><pre id='PsyoK'><center id='PsyoK'></center></pre></bdo></b><th id='PsyoK'></th></span></q></dt></tr></i><div id='PsyoK'><tfoot id='PsyoK'></tfoot><dl id='PsyoK'><fieldset id='PsyoK'></fieldset></dl></div>

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

                  问题描述

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

                  ErrorException (E_DEPRECATED)无括号的`a ?乙:丙?d : e` 已弃用.使用`(a ? b : c) ?d : e` 还是 `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] : '';}

                  有人能帮我解决这个问题吗?

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

                  解决方案

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

                  弃用警告在此处转载:

                  代码:

                  $allLanguages = ['en', 'es', 'fr'];$values = ['es' =>'西班牙语1'];$文件内容 = ['富' =>['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

                  数组 ('en' =>'','es' =>'西班牙语2','fr' =>'法语',)

                  作为脚本的人类读者,我会假设从左到右读取您的条件——但这会将 Spanish1 作为输出值.

                  即使在 php7.4 之前,输出也是Spanish2,因为后者在决策树中的分叉被优先考虑.

                  为避免这种情况,您必须将条件括在括号中,以准确说明应如何处理执行顺序.

                  另外,我同意@Laurel 的观点,在 php7 中是时候让您接受空合并运算符的句法甜蜜了.这将避免优先级问题和使用括号的需要,但根据您想要的结果,您可能需要对条件重新排序.

                  优先于$values:(Demo)

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

                  优先于$filesContent:(Demo)

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

                  附言IIRC,php 手册建议不要在代码清晰的基础上使用这样的嵌套三元组/条件.我不介意这种情况,我喜欢避免代码膨胀,但其他开发人员可能会采取更纯粹的立场.

                  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.

                  这篇关于PHP 错误:未加括号的 `a ?乙:丙?d : e` 已弃用.使用`(a ? b : c) ?d : e` 还是 `a ?b : (c ? d : e)`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:php循环计数器引导行 下一篇:使用 PHP glob 获取文件夹 - 深度无限

                  相关文章

                  最新文章

                  • <bdo id='UhMAl'></bdo><ul id='UhMAl'></ul>

                    <small id='UhMAl'></small><noframes id='UhMAl'>

                  1. <legend id='UhMAl'><style id='UhMAl'><dir id='UhMAl'><q id='UhMAl'></q></dir></style></legend>
                    <i id='UhMAl'><tr id='UhMAl'><dt id='UhMAl'><q id='UhMAl'><span id='UhMAl'><b id='UhMAl'><form id='UhMAl'><ins id='UhMAl'></ins><ul id='UhMAl'></ul><sub id='UhMAl'></sub></form><legend id='UhMAl'></legend><bdo id='UhMAl'><pre id='UhMAl'><center id='UhMAl'></center></pre></bdo></b><th id='UhMAl'></th></span></q></dt></tr></i><div id='UhMAl'><tfoot id='UhMAl'></tfoot><dl id='UhMAl'><fieldset id='UhMAl'></fieldset></dl></div>
                    <tfoot id='UhMAl'></tfoot>