2016-03-14 150 views
0

我的服務器錯誤日誌中顯示以下錯誤:修復「非法字符串偏移」?

[03-Feb-2016 09:04:11 UTC] PHP Warning: Illegal string offset 'results-page' in /home/myaccount/public_html/wp-content/plugins/simple-author-search/simple-author-search.php on line 35 

這是一種自我創作的WordPress插件,它可以讓訪問者搜索WordPress用戶在前端。受影響的代碼如下,第35行是開始「$ form =」的行。

function showForm($atts = array()) { 
    $keywords = filter_input(INPUT_GET, 'sas-keywords'); 
    $form = '<form action="' . get_permalink($atts['results-page']) . '"name="sas-form" class="sas-form form-inline pull-xs-right"><input type="hidden" name="sas-search" value="1"><input type="text" class="form-control" name="sas-keywords" value="' . htmlentities($keywords) . '"/>' . '&nbsp;<input type="submit" value="Search" class="btn btn-secondary" /></form>'; 
    return $form; 
} 

請你幫我理解爲什麼文本結果頁面導致這個錯誤?

謝謝你的時間。

+1

'$ atts'在發生這種情況時不是數組。 – mario

回答

1

非法字符串偏移意味着,您希望使用非法鍵訪問字符串中的偏移量。因爲一個字符串永遠不可能是一個字符串的合法偏移量,所以這將以你的警告結束。

在你的情況下,你必須確保$atts是一個數組,密鑰results-page存在。

function showForm($atts = array()) { 
    if (!is_array($atts) || !isset($atts['results-page'])) { 
     // invalid argument - do some error handling 
     return ''; 
    } 

    $keywords = filter_input(INPUT_GET, 'sas-keywords'); 
    $form = '<form action="' . get_permalink($atts['results-page']) . '"name="sas-form" class="sas-form form-inline pull-xs-right"><input type="hidden" name="sas-search" value="1"><input type="text" class="form-control" name="sas-keywords" value="' . htmlentities($keywords) . '"/>' . '&nbsp;<input type="submit" value="Search" class="btn btn-secondary" /></form>'; 
    return $form; 
} 
+0

菲利普 - 非常感謝。害怕我沒有足夠的知識來充分理解解釋 - 但是增加這些檢查似乎可以消除錯誤。我正在檢查$ atts是否是一個數組以及結果頁是否存在,好吧。謝謝。 –