Page 1 of 1

Incrementing array variables

Posted: Wed Aug 25, 2010 12:11 pm
by fredsnertz
I have a script with a line like this:

Code: Select all

$array[$id]++;
I use the web server error logs for debugging and apparently the warnings are tuned way up. Everytime a new value of $id is encountered, I get a warning saying I'm incrementing a variable that doesn't exist. I can do this:

Code: Select all

if (isset($array[$id])) $array[$id]++; else $array[$id]=1;
but that seems like overkill. I can't initialize the array because I have no way to predict the values of $id. Is there a more correct way to do what I'm trying to do or is that second line the best way? Thanks.

Re: Incrementing array variables

Posted: Wed Aug 25, 2010 12:31 pm
by AbraCadaver
If $id = 5 and $array[5] doesn't exist then you can't increment it without a notice.