首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用变量变量时未初始化的字符串偏移通知

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

Stack Overflow用户
提问于 2010-02-17 21:05:06
回答 2查看 13K关注 0票数 1

当运行以下代码时,一切正常,直到$i = 5。之后,我收到未初始化的字符串偏移通知,即使数组似乎被正确填充。我在本地运行时收到此通知,但在远程服务器上检查时未收到此通知。两者都在运行v5.2.11。根据错误报告配置,我假设从本地到远程的输出是不同的,但是是什么导致了这个通知呢?

代码:

代码语言:javascript
复制
$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++;
}

输出:

代码语言:javascript
复制
$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

提前感谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-02-17 23:02:46

好吧,我真的不确定这是什么目的,所以很难说……但是我怀疑这个问题可以通过使用带大括号的变量来解决。它试图使用$thisRow$i作为变量名,但这并不存在。

代码语言:javascript
复制
$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
EN

Stack Overflow用户

发布于 2011-06-22 01:54:21

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2280797

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档