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.

5
  • Is it an option for you to just turn off PHP Notices? Commented Nov 1, 2012 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? Commented Nov 1, 2012 at 20:19
  • array_key_exists + isset if you want to do it the real hardcore way ;) Commented Nov 1, 2012 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. Commented Nov 1, 2012 at 20:28
  • possible duplicate of PHP: "Notice: Undefined variable" and "Notice: Undefined index" Commented Mar 29, 2013 at 12:16

3 Answers 3

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
}
Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

0

Old/Deprecaded/Unrecommended but the shortest solution is

@$myArray['test'] += $myValue;

5 Comments

Is this black magic? It still works as of 2021
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!
@thedude12 YES :D
@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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.