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

时间:2010-02-17 13:05:06

标签: php arrays variables

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

提前致谢!

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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)刚刚关闭。