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.
+=within the PHP 8 limitations of warnings being given when trying to add to an unset value. This causes code bloat esp. for dynamically generated values. Are there any workarounds?