views:

328

answers:

1

When the run the following code, everything works fine until $i = 5. After that I get Uninitialized string offset notices, even though the arrays seem to be getting populated correctly. I get this notice when running locally but not when I check it on the remote server. Both are running v5.2.11. I assume the output is different from local to remote based on error reporting configs, but what is causing the notice?

Code:

$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++;
}

Output:

$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

Thanks in advance!

A: 

Ok, I'm not really sure what the purpose of this is, so it's hard to say... but I suspect this will be solved by using braces with your variable variables. It's trying to use $thisRow[$i] as the variable name, but that doesn't exist.

$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++;
}

This worked with ripDigits being initialized to an array of 6 numbers.

Also, as to why it 'works' on the server - likely error reporting (E_NOTICE) is just turned off.

John Deerhake
Thanks John. I'll give that a try a bit later. I tried the braces before but I did it as {$$thisRow}. I'll loop back and update the results here.I agree on the error reporting.
Eric