3

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

Recently turned on errors and I'm catching the Undefined Index and Undefined Offset error on lines I'm incrementing to a new array index.

Here's a very basic example.

for($i = 0; $i<10; $i++)
{
    $arr['var1'] += $val1[$i];
    $arr['var2'] += $val2[$i];
}

I'm getting the error because on the first iteration $arr['var1'] isn't set.

I've found that both checking that the index is set

if (!isset($arr['var1'])) {
    $arr['var1'] = 0;
}
$arr['var1'] += $val1[$i];

• and •

automatically setting the index with a val of 0 before the incrementing forloop both stop the error messages.

My question is that I'll have about 150 of these to fix, what would be the best way to approach this problem. Check isset on each one, or define each one beforehand with a val of 0?

Community
  • 1
  • 1
Jeremy A
  • 131
  • 4
  • 15
  • [Notice: Undefined index when trying to increment an associative array in PHP](https://stackoverflow.com/q/8062052/2943403) – mickmackusa Oct 10 '22 at 09:06

5 Answers5

4

You should always initialize your variables. This is absolutely a best practice across virtually every language, and it's considered an extremely bad habit to access unset variables. You should always be developing software with errors on.

Your code should read:

$arr['var1'] = $arr['var2'] = 0;
for($i = 0; $i<10; $i++)
{
    $arr['var1'] += $val1[$i];
    $arr['var2'] += $val2[$i];
}

Take the hit, fix all 150 occurrences of this problem, and then learn from your mistake. Write the code correctly the next time.

user229044
  • 232,980
  • 40
  • 330
  • 338
1

This is personal preference, as the same functionality results from either choice. However, declaring it ahead of time will save you a conditional and a function call (which would be in the loop, repeated), so from an efficiency standpoint it's best to declare it before the loop. Typically, best practice is to declare variables before you use them. However, PHP lets you get away with things like this, which is why it's not required.

nickb
  • 59,313
  • 13
  • 108
  • 143
1

First initialize, then iterate:

$arr['var1'] = 0;
$arr['var2'] = 0;
for($i = 0; $i<10; $i++)
{
    $arr['var1'] += $val1[$i];
    $arr['var2'] += $val2[$i];
}

This is much more readable code - better practice then what you have been doing.

If you have to init an array that holds many counters you might do something like this:

$arr = array('counter1' => 0, 'counter2' => 0, 'counter3' => 0);
ddinchev
  • 33,683
  • 28
  • 88
  • 133
1

The array entries will come into being (autovivification) if you access them through references. Thus:

for($i = 0; $i < 10; $i++) {
    $var1 =& $arr['var1'];
    $var2 =& $arr['var2'];
    $var1 += $val1[$i];
    $var2 += $val2[$i];
}

Even the array itself doesn't need to be declared. By default, the new entry will have the value NULL, which gets converted to 0 on the addition.

cleong
  • 7,242
  • 4
  • 31
  • 40
0

You should fix these areas, just for good coding practice. The approach you take would really depend on your code. If you are truly working with new variables, then just define them as false, 0, '', or whatever makes sense as a default for the variable in question.

If you are at a point in the code where you may or may not have a value set (such as evaluating $_POST or $_GET arrays) then use isset(), empty() or similar to evaluate it.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103