【问题标题】:Uninitialized string offset notice when using variable variables使用变量变量时未初始化的字符串偏移通知
【发布时间】:2011-01-17 20:34:33
【问题描述】:

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

提前致谢!

【问题讨论】:

    标签: php arrays variables


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 谢谢约翰。稍后我会尝试一下。我之前尝试过大括号,但我是用 {$$thisRow} 做的。我将在此处循环并更新结果。我同意错误报告。
      猜你喜欢
      • 1970-01-01
      • 2019-08-11
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多