0

In my php applicaiton I have the following code:

try {
    $ordersIngredients[$userIngredient->getId()][$day] += $quantity;
} catch (\Exception $e) {
    ~r(array(
        $ordersIngredients[$userIngredient->getId()],
        $day,
        array_key_exists($day, $ordersIngredients[$userIngredient->getId()]),
        $e->getMessage()
    ));
}

The r() function prints the following:

array(4)
    0   =>  array(4)
        0   =>  0.9
        1   =>  null
        2   =>  null
        3   =>  1
    )
    1   =>  3
    2   =>  true
    3   =>  Notice: Undefined offset: 3
)

How can I have an error on un undefined offset when the offset actually exists given the dump of the array and array_key_exists ?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Sébastien
  • 5,263
  • 11
  • 55
  • 116
  • 1
    Side note: How do you have a function name that starts with `~`? – phpisuber01 Sep 03 '15 at 18:23
  • 1
    @phpisuber01: Not part of the name http://php.net/manual/en/language.operators.bitwise.php – AbraCadaver Sep 03 '15 at 18:25
  • ~r (REF) is a library for dumping stuff – Sébastien Sep 03 '15 at 18:27
  • Ha, you're right, very sneaky I didn't even consider that since its not assigned or evaluated. /dunce-cap – phpisuber01 Sep 03 '15 at 18:28
  • @RocketHazmat: the first element I am dumping is $ordersIngredients[$userIngredient->getId()] which is an array whith the key $day=3 – Sébastien Sep 03 '15 at 18:29
  • @Sébastien: Yeah, I just realized that. It wasn't clear what the array dump shown represented. I edited the question for you to hopefully make that more clear. – gen_Eric Sep 03 '15 at 18:30
  • thanks! it's killing me. It's telling me there is a bug and there is not at the same time! – Sébastien Sep 03 '15 at 18:36
  • 2
    How is your `catch` catching this? It's a `notice` not an exception/error. – gen_Eric Sep 03 '15 at 18:42
  • 1
    @RocketHazmat Maybe he's defined a custom error handler, as in http://stackoverflow.com/questions/5373780/how-to-catch-this-error-notice-undefined-offset-0 – Barmar Sep 03 '15 at 18:50
  • @Barmar: What if the exception is in the error handler instead of in this code? – gen_Eric Sep 03 '15 at 18:51
  • @RocketHazmat I just tested, and the error in the error handler will be reported by this code. – Barmar Sep 03 '15 at 18:53
  • @Sébastien: If you are creating a custom error handler... can you show that to us? I'm curious if that's where the issue lies. – gen_Eric Sep 03 '15 at 18:54
  • found it, thanks to your notice clue ! The array is initiated only with keys 0,1,2 but the += somehow adds another one before the error is thrown, hence the confusion – Sébastien Sep 03 '15 at 18:55

1 Answers1

1

The issue here is that you were trying to append to a value that did not exist in the array.

$ordersIngredients[$userIngredient->getId()][$day] += $quantity;

This is the same as writing:

$ordersIngredients[$userIngredient->getId()][$day] = 
    $ordersIngredients[$userIngredient->getId()][$day] + $quantity;

So, what's happening is that PHP is trying to read the value at index 3, but it can't. That's what causes your notice. Since it's only a notice, PHP treats the undefined index as null and keeps going. It then adds null + $quantity to your array.

After finishing the line, it moves into your error handler and then to your catch block.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337