3

I'm working on another developers old code and there are tons of Notice: Undefined index errors when data is being set using the += operator. Essentially the index is not set yet in the array so the error is generated.

Example:

$myArray = array();
$myValue = 1;
for ($i = 1; $i <= 10; $i++) 
{
    $myArray['test'] += 1;
}

Will generate an error on the first run since the test index is not set yet.

I know I can remove this error with the following code:

$myArray = array();
$myValue = 1;
for ($i = 1; $i <= 10; $i++) 
{
    if ( ! isset($myArray['test']) )
    {
        $myArray['test'] = $myValue;
    } 
    else 
    {
        $myArray['test'] += $myValue;
    }
}

However, there are around 50 of things like this to change. Is it worth writing all these isset statements or is there a better way I am not aware of?

EDIT: I should note that the array indexes aren't always the same and sometimes aren't set so I can't preset the index in this circumstance to avoid the error.

hakre
  • 193,403
  • 52
  • 435
  • 836
bobcat
  • 165
  • 1
  • 11
  • Is it an option for you to just turn off PHP Notices? – luttkens Nov 01 '12 at 20:12
  • All errors are off on the production server. I just noticed these errors after having to edit something on my development server. Would it be OK to ignore these then? – bobcat Nov 01 '12 at 20:19
  • `array_key_exists` + `isset` if you want to do it the real hardcore way ;) – clentfort Nov 01 '12 at 20:25
  • I would try to get away from them. If you avoid them in general, the ones you get (unexpectedly) are quite helpful. – luttkens Nov 01 '12 at 20:28
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Jocelyn Mar 29 '13 at 12:16

3 Answers3

5

This is a bit shorter, but perhaps still a bit complicated if you have many edits.

$myArray = array();
$myValue = 1;
for ($i = 1; $i <= 10; $i++) 
{
    isset($myArray['test']) ? $myArray['test'] += $myValue : $myArray['test'] = $myValue;

}

You could also write a global function (not tested)..

$myArray = array();   
$myValue = 1;
for ($i = 1; $i <= 10; $i++) 
{
    increment($myArray['test'], $myValue);
}

function increment(&$var, $inc){
    $var = isset($var) ? $var += $inc : $var = $inc
}
luttkens
  • 1,272
  • 8
  • 16
4

If you are using PHP 7 and above, you can use the Null Coalescing Operator to make this code cleaner.

$myArray = [];
$myValue = 1;
for ($i = 1; $i <= 10; $i++) 
{
    $myArray['test'] = $myValue + ($myArray['test'] ?? 0);
}

The benefit here is not only that the code is cleaner, but you're also being more explicit about the default value (0)

Tyler V.
  • 2,471
  • 21
  • 44
0

Old/Deprecaded/Unrecommended but the shortest solution is

@$myArray['test'] += $myValue;
WOLFF
  • 586
  • 4
  • 7
  • 1
    Is this black magic? It still works as of 2021 – thedude12 Feb 20 '21 at 10:42
  • Don't do this! (WOLFF did say "Unrecommended".) If someone comes along and replaces $myValue with myFunc() and that function errors, this will gobble up the error. Very bad practice! – Tyler V. Mar 19 '21 at 02:28
  • @thedude12 YES :D – WOLFF Jun 28 '21 at 04:06
  • @thedude12 It doesn't "work", it just hides the error. Then a few years later, an unknown unknown happens, and that line of code causes a fatal error, the software doesn't work anymore, and nothing shows in the logs. Then you spend a lot of hours until eventually you find the culprit. Moral of the story, don't use error suppression. – Wadih M. Jun 02 '22 at 16:45
  • still working in 2022 in php 8.1, if You are creating some aside script and You want to do it fast, You can use this then. – WOLFF Jun 08 '22 at 08:58