PHP 错误 Unparenthesized `a ? b : c ? d : e` is not supported. Use eithe

最近在一个新的服务器上测试一些老代码的时候得到了类似上面的错误:

[Thu Apr 25 07:37:34.139768 2024] [php:error] [pid 691410] [client 192.168.1.229:57183] PHP Fatal error:  Unparenthesized `a ? b : c ? d : e` is not supported. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)` in /var/www/html/massnet/wp-content/themes/enfold/functions-enfold.php on line 265
[Thu Apr 25 07:37:35.883933 2024] [php:error] [pid 691415] [client 192.168.1.229:57182] PHP Fatal error:  Unparenthesized `a ? b : c ? d : e` is not supported. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)` in /var/www/html/massnet/wp-content/themes/enfold/functions-enfold.php on line 265
[Thu Apr 25 07:37:36.665729 2024] [php:error] [pid 691411] [client 192.168.1.229:57189] PHP Fatal error:  Unparenthesized `a ? b : c ? d : e` is not supported. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)` in /var/www/html/massnet/wp-content/themes/enfold/functions-enfold.php on line 265
[Thu Apr 25 07:37:36.864684 2024] [php:error] [pid 691412] [client 192.168.1.229:57191] PHP Fatal error:  Unparenthesized `a ? b : c ? d : e` is not supported. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)` in /var/www/html/massnet/wp-content/themes/enfold/functions-enfold.php on line 265
[Thu Apr 25 07:37:37.059653 2024] [php:error] [pid 691413] [client 192.168.1.229:57193] PHP Fatal error:  Unparenthesized `a ? b : c ? d : e` is not supported. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)` in /var/www/html/massnet/wp-content/themes/enfold/functions-enfold.php on line 265
[Thu Apr 25 07:37:37.266231 2024] [php:error] [pid 691414] [client 192.168.1.229:57195] PHP Fatal error:  Unparenthesized `a ? b : c ? d : e` is not supported. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)` in /var/www/html/massnet/wp-content/themes/enfold/functions-enfold.php on line 265
[Thu Apr 25 07:37:37.474902 2024] [php:error] [pid 691410] [client 192.168.1.229:57197] PHP Fatal error:  Unparenthesized `a ? b : c ? d : e` is not supported. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)` in /var/www/html/massnet/wp-content/themes/enfold/functions-enfold.php on line 265
~                                                                                                                                                                                                                                                                                                             

错误原因

a ? b : c 这是一种常见的三元运算符 意思是如果 a成立那么结果就返回b 否则返回c

a ? b : c ? d : e 当你这么写时 程序就会有多种理解了

一种是 (a ? b : c) ? d : e 把前面的当做了一个整体

另一种是 a ? b : (c ? d : e) 把后面的当做了一个整体

在 PHP 8 中,这种错误被标记为严重错误。

出现上面的原因是 PHP 8 升级的问题。

这个问题出现在我们 WP 安装的插件上,为了避免这个问题,要么需要更新插件的版本,要么就不要更新 PHP 的版本。

感觉 PHP 8 升级后,不少插件的作者都没有来得及更新,也引起了不少的问题。