2010-02-17 127 views
1

當運行以下代碼時,一切正常,直到$ i = 5。之後,我得到了未初始化的字符串偏移量通知,即使數組似乎正在正確填充。我在本地運行時收到此通知,但在遠程服務器上檢查時未收到此通知。兩者都運行v5.2.11。基於錯誤報告配置,我假設輸出不同於本地到遠程,但是是什麼導致了通知?使用變量變量時未初始化的字符串偏移量通知

代碼:

$i = 0; 
$row = 0; 
$col = 0; 
$quad = 0; 
while(count($ripDigits) > 0) 
{ 
    $ranNum = rand(0, count($ripDigits) - 1); 
    $ripDigit_splice = array_splice($ripDigits, $ranNum, 1); 
    $ranDigit = $ripDigit_splice[0]; 
    echo ("\$i = " . $i . " | count(\$ripDigits) = " . count($ripDigits) . "<br />\n"); 

    $thisRow = "row_" . $row; 
    $$thisRow[$i] = $ranDigit; 

    echo ("\t\t<td><b>" . $$thisRow[$i] . "</b></td>\n"); 

    $thisUsedColumn = "usedDigits_column_" . $i; 
    $$thisUsedColumn[$col] = $$thisRow[$i]; 

    $thisUsedColumn = "usedDigits_quad_" . $i; 
    $$thisUsedColumn[$quad] = $$thisRow[$i]; 

    $i++; 
} 

輸出:

$i = 0 | count($ripDigits) = 8 
$i = 1 | count($ripDigits) = 7 
$i = 2 | count($ripDigits) = 6 
$i = 3 | count($ripDigits) = 5 
$i = 4 | count($ripDigits) = 4 
$i = 5 | count($ripDigits) = 3 

Notice: Uninitialized string offset: 5 in script.php on line 97 

Notice: Uninitialized string offset: 5 in script.php on line 99 

Notice: Uninitialized string offset: 5 in script.php on line 102 

Notice: Uninitialized string offset: 5 in script.php on line 105 
$i = 6 | count($ripDigits) = 2 

Notice: Uninitialized string offset: 6 in script.php on line 97 

Notice: Uninitialized string offset: 6 in script.php on line 99 

Notice: Uninitialized string offset: 6 in script.php on line 102 

Notice: Uninitialized string offset: 6 in script.php on line 105 
$i = 7 | count($ripDigits) = 1 

Notice: Uninitialized string offset: 7 in script.php on line 97 

Notice: Uninitialized string offset: 7 in script.php on line 99 

Notice: Uninitialized string offset: 7 in script.php on line 102 

Notice: Uninitialized string offset: 7 in script.php on line 105 
$i = 8 | count($ripDigits) = 0 

Notice: Uninitialized string offset: 8 in script.php on line 97 

Notice: Uninitialized string offset: 8 in script.php on line 99 

Notice: Uninitialized string offset: 8 in script.php on line 102 

Notice: Uninitialized string offset: 8 in script.php on line 105 

1 8 4 2 7 5 9 3 6 

提前感謝!

+0

什麼是ripDigits? – user187291 2010-02-17 13:46:13

回答

0

好的,我不太確定這是什麼目的,所以很難說......但我懷疑這可以通過在變量變量中使用大括號來解決。它試圖使用$ thisRow [$ i]作爲變量名稱,但不存在。

$i = 0; 
$row = 0; 
$col = 0; 
$quad = 0; 
while(count($ripDigits) > 0) { 
    $ranNum = rand(0, count($ripDigits) - 1); 
    $ripDigit_splice = array_splice($ripDigits, $ranNum, 1); 
    $ranDigit = $ripDigit_splice[0]; 
    echo ("\$i = " . $i . " | count(\$ripDigits) = " . count($ripDigits) . "<br />\n"); 

    $thisRow = "row_" . $row; 
    ${$thisRow}[$i] = $ranDigit; 

    echo ("\t\t<td><b>" . ${$thisRow}[$i] . "</b></td>\n"); 

    $thisUsedColumn = "usedDigits_column_" . $i; 
    ${$thisUsedColumn}[$col] = ${$thisRow}[$i]; 

    $thisUsedColumn = "usedDigits_quad_" . $i; 
    ${$thisUsedColumn}[$quad] = ${$thisRow}[$i]; 

    $i++; 
} 

這與ripDigits被初始化爲6數組數組。

此外,至於爲什麼它在服務器上「工作」 - 可能的錯誤報告(E_NOTICE)剛剛關閉。

+0

謝謝約翰。稍後我會試一試。我之前嘗試過大括號,但是我做了{$$ thisRow}。我會循環回來並在這裏更新結果。 我同意錯誤報告。 – Eric 2010-02-19 15:38:09

1

我想你的服務器和本地機器的php版本是不同的。今天我也收到了這條消息,如果你使用xammp v.1.7.4,你會得到這條消息,但是如果你使用1.7.3,你不會。