2017-02-14 37 views
0

由於某種原因,我在For循環中出現錯誤。 我只想通過php中的數組向後走。PHP致命錯誤:For循環中不支持的操作數類型

瀏覽器中的錯誤是:

Fatal error: Unsupported operand types in /var/html/modules/getChat.php on line 18

線在此部分代碼1號線18:

這裏是代碼:

for($x = sizeof($result-1); $x > 0; $x--) 
{ 
    echo '<div class="message '.$result[$x].'"> <img src="'.$result[$x].'" /><span class="name">'.$result[$x].'</span> 
    <p>'.$result[$x].'</p> 
    </div>'; 
} 

我希望你能幫助

Thanks Max

+2

真的好像是'sizeof($ result) - 1'。 – raina77ow

+0

非常感謝我用sizeof($ result)修復了它 - 1這也是一個愚蠢的錯誤 – Arayni

回答

1

$result是一個數組,並且從數組中減去1沒有任何意義。你可能想用這個代替:

for ($x = sizeof($result) - 1; $x > 0; $x--) // ... 

是的,它似乎是你無意中跳過你的數組的第一個元素在這裏。如果是的話,修復的條件($x >= 0) - 或者只是緊湊的整個循環到while

$x = count($result); 
while($x--) { 
    // output with $result[$x] 
} 

如果這不是一個瓶頸(和最有可能它不是),你最好顯示代碼與array_reverse的真正意圖:

foreach (array_reverse($result) as $el) { 
    // output with $el 
} 
+1

另外'x> 0'不會使用數組的第一個元素。也許OP有意這樣做,但仍然如此。 –

相關問題