当运行以下代码时,一切正常,直到$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提前感谢!
发布于 2010-02-17 23:02:46
好吧,我真的不确定这是什么目的,所以很难说……但是我怀疑这个问题可以通过使用带大括号的变量来解决。它试图使用$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)刚刚被关闭。
发布于 2011-06-22 01:54:21
我猜你的服务器和本地机器的php版本是不同的。今天我也收到了这个消息,我告诉你如果你使用xammp v.1.7.4你会收到这个消息,但是如果你使用1.7.3你就不会得到这个消息。
https://stackoverflow.com/questions/2280797
复制相似问题