簡體   English   中英

php 7.2升級錯誤“無法將空字符串分配給字符串偏移量”

[英]php 7.2 upgrade error “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轉換為數組,也不(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。

除非您想使所有腳本現代化,否則請忽略它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM