cantera
cantera

Reputation: 25015

PHP Shorthand Addition Operator - Undefined Offset

I'm using the PHP shorthand addition operator to tally the number of times a specific id occurs within a multidimensional array:

$source['tally'] = array();

foreach ($items as $item) {
    $source['tally'][$item->getId()] += 1;
}

The first time it hits a new id, it sets its 'tally' value to 1 and then increments it each time it's found thereafter.

The code works perfectly (I get the correct totals), but PHP gives me an "Undefined Offset" notice each time it finds a new id.

I know I can just turn off notices in php.ini, but figured there must be a reason why PHP doesn't approve of my technique.

Is it considered bad practice to create a new key/offset dynamically like this, and is there a better approach I should take instead?

Please Note: To help clarify following initial feedback, I do understand why the notice is being given. My question is whether I should do anything about it or just live with the notice. Apologies if my question didn't make that clear enough.

Upvotes: 4

Views: 5400

Answers (4)

Gustav Bertram
Gustav Bertram

Reputation: 14905

If you simply want to hide the notice, you can use the error control operator:

$source['tally'] = array();

foreach ($items as $item) {
    @$source['tally'][$item->getId()]++;
}

However, you should generally initialize your variables, in this case by adding the following code inside the loop:

if (!isset( $source['tally'][$item->getId()] ))
{
   $source['tally'][$item->getId()] = 0;
}

Upvotes: 0

Álvaro González
Álvaro González

Reputation: 146588

You have to understand that PHP notices are a tool. They exist so you have additional help when writing code and you are able to detect potential bugs easily. Uninitialized variables are the typical example. Many developers ask: if it's not mandatory to initialize variables, why is PHP complaining? Because it's trying to help:

$item_count = 0;
while( do_some_stuff() ){
     $iten_count++; // Notice: Undefined variable: iten_count
}
echo $item_count . ' items found';

Oops, I mistyped the variable name.

$res = mysql_query('SELECT * FROM foo WHERE foo_id=' . (int)$_GET['foo_id']);
// Notice: Undefined index: foo_id

Oops, I haven't provided a default value.

Yours is just another example of the same situation. If you're incrementing the wrong array element, you'd like to know.

Upvotes: 5

Madara's Ghost
Madara's Ghost

Reputation: 175048

It's caused because you don't initialize your array to contain the initial value of 0. Note that the code would probably work, however it is considered good practice to initialize all the variable you are about to preform actions upon. So the following code is an example of what you should probably have:

<?php
    $source['tally'] = array();

    foreach ($items as $item) {
        //For each $item in $items,
        //check if that item doesn't exist and create it (0 times).
        //Then, regardless of the previous statement, increase it by one.
        if (!isset($source['tally'][$item->getID()]) $source['tally'][$item->getID()] = 0;
        $source['tally'][$item->getId()] += 1;
    }
?>

The actual reason why PHP cares about it, is mainly to warn you about that empty value (much like it would if you try to read it). It is a kind of an error, not a fatal, script-killing one, but a more subtle quiet one. You should still fix it though.

Upvotes: 3

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799230

Using += (or any of the other augmented assignment operators) assumes that a value already exists for that key. Since this is not the case the first time the ID is encountered, a notice is emitted and 0 is assumed.

Upvotes: 4

Related Questions