使用变量变量时未初始化的字符串偏移量通知

问题描述:

当运行以下代码时,一切正常,直到$ 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

好的,我不太确定这是什么目的,所以很难说......但我怀疑这可以通过在变量变量中使用大括号来解决。它试图使用$ 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

我想你的服务器和本地机器的php版本是不同的。今天我也收到了这条消息,如果你使用xammp v.1.7.4,你会得到这条消息,但是如果你使用1.7.3,你不会。