PHP 7.2 升级错误"Cannot assign an empty string to a string offset"



我正在使用一个我非常喜欢的老式wordpress主题,我只有一些基本的编码技能。我的提供商强行将我的服务器 php 版本升级到 7.2,当然我的一些脚本正在崩溃。

public function localize( $handle, $object_name, $l10n ) {
    if ( $handle === 'jquery' )
        $handle = 'jquery-core';
    if ( is_array($l10n) && isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present
        $after = $l10n['l10n_print_after'];
        unset($l10n['l10n_print_after']);
    }
    foreach ( (array) $l10n as $key => $value ) {
        if ( !is_scalar($value) )
            continue;
        $l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8');
    }``

根据日志,错误在最后一行,因为显然它"无法将空字符串分配给字符串偏移量"

也许这比改变一件简单的事情要复杂得多......有什么解决方案吗?

这里最合乎逻辑的做法是将其更改为在in_array条件下更新:

if ( is_array($l10n){
    if(isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present
        $after = $l10n['l10n_print_after'];
         unset($l10n['l10n_print_after']);
    }
    foreach ($l10n as $key => $value ) {
        if ( !is_scalar($value) )
            continue;
        $l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8');
    }
}

即使您将(array)$l10n转换为数组,这也不会将变量本身设置为数组(如 $l10n = (array)$l10n )。

也就是说,使用混合类型可能非常麻烦。 最好只发送数组或先处理数组位,这样你就有一个一致的类型。喜欢这个:

public function localize( $handle, $object_name, $l10n ) {
    //normalize arguments
    if(!is_array($l10n)) $l10n = [$l10n];
    if ( $handle === 'jquery' )
        $handle = 'jquery-core';
    if ( isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present
        $after = $l10n['l10n_print_after'];
        unset($l10n['l10n_print_after']);
    }
    foreach ($l10n as $key => $value ) {
        if ( !is_scalar($value) )
            continue;
        $l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8');
    }
}

解决此问题的另一种可能方法是强制您的服务器运行旧版本的 php,如果他们可以这样做的话。例如,使用 pantheon.io 您可以强制服务器在服务器配置文件中运行特定版本的 php。

除非您想对所有脚本进行现代化改造,否则请忽略这一点。

最新更新