如何给下面的 php 行加上括号?

How to parenthesized the following php line?

我真的对PHP一无所知,同样的错误在stack overflow上搜索后,我自己也找不到解决方案(我知道解决方案是错误的,但我不知道不知道把括号放在哪里)。这个错误让我的网站瘫痪了。

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/vhosts/[domain]/httpdocs/wp-content/themes/Avada/includes/avada-functions.php on line 443.

第 443 行是:

$items .= '<input type="hidden" name="redirect" value="' . esc_url( ( isset( $_SERVER['HTTP_REFERER'] ) ) ? esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '' ) . '">';

本例中的问题是使用嵌套的三元运算符。错误说你应该在括号中明确定义哪些元素属于哪个表达式。

代码中的附加括号应放在以下表达式周围

(isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '')

所以你的行应该是这样的

$items .= '<input type="hidden" name="redirect" value="' . esc_url( ( isset( $_SERVER['HTTP_REFERER'] ) ) ? esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : (isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '') ) . '">';

相反,我建议采用不同的方法 - 将这一行分成更简单的元素,就像这样

$ternaryTrue = esc_url_raw(wp_unslash($_SERVER['HTTP_REFERER']));
$ternatyFalse = isset($_SERVER['REQUEST_URI']) ? esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'])) : '';
$value = isset($_SERVER['HTTP_REFERER']) ? $ternaryTrue : $ternatyFalse;
$items .= '<input type="hidden" name="redirect" value="' . esc_url($value) . '">';