Vel Posted July 9, 2012 Share Posted July 9, 2012 This is more of a question of "is it OK and / or correct" and a point of interest to myself (and hopefully others) than anything else. I have a situation where in a multidimensional array I am counting the number of entries in another part of this array. I have this statement: if($array[$i]['counter']) $array[$i]['counter']++; else $array[$i]['counter'] = 1; It would make sense to me to cut the code down and remove the if statement, and just go with: $array[$i]['counter']++; Completely ignoring the step of checking to see if it's initialised, and if not initialising it. I'm guessing this isn't totally proper, but is it completely incorrect to do this or is it an acceptable way of saving a couple of lines of code? Quote Link to comment https://forums.phpfreaks.com/topic/265455-more-of-a-theory-question-on-uninitiated-variables/ Share on other sites More sharing options...
Adam Posted July 9, 2012 Share Posted July 9, 2012 Neither are actually correct. The first example will generate an 'undefined index' notice-level error if the key doesn't exist - you should be using isset. The second does not define the initial value, so what are you expecting to increment? Correct way is: if (!isset($array[$i]['counter'])) { $array[$i]['counter'] = 0; } $array[$i]['counter']++; Quote Link to comment https://forums.phpfreaks.com/topic/265455-more-of-a-theory-question-on-uninitiated-variables/#findComment-1360421 Share on other sites More sharing options...
requinix Posted July 9, 2012 Share Posted July 9, 2012 Also, the correct way for the first example would be to use if(isset(...)) (like how Adam did, but without the ! because the logic is backwards). Quote Link to comment https://forums.phpfreaks.com/topic/265455-more-of-a-theory-question-on-uninitiated-variables/#findComment-1360423 Share on other sites More sharing options...
Vel Posted July 9, 2012 Author Share Posted July 9, 2012 Neither are actually correct. The first example will generate an 'undefined index' notice-level error if the key doesn't exist - you should be using isset. The second does not define the initial value, so what are you expecting to increment? Correct way is: if (!isset($array[$i]['counter'])) { $array[$i]['counter'] = 0; } $array[$i]['counter']++; Sorry, I mistyped it in my haste. I do use isset to check if it exists, and if it does increment it by 1, otherwise set it to 1. Quote Link to comment https://forums.phpfreaks.com/topic/265455-more-of-a-theory-question-on-uninitiated-variables/#findComment-1360424 Share on other sites More sharing options...
silkfire Posted July 9, 2012 Share Posted July 9, 2012 In PHP, even if null is evaluated as 0, something that is undefined will throw an error before you get the chance to increment it. Quote Link to comment https://forums.phpfreaks.com/topic/265455-more-of-a-theory-question-on-uninitiated-variables/#findComment-1360432 Share on other sites More sharing options...
Vel Posted July 10, 2012 Author Share Posted July 10, 2012 Thanks for the advice guys. I appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/265455-more-of-a-theory-question-on-uninitiated-variables/#findComment-1360468 Share on other sites More sharing options...
Adam Posted July 10, 2012 Share Posted July 10, 2012 In PHP, even if null is evaluated as 0, something that is undefined will throw an error before you get the chance to increment it. Yeah, although even relying on those kind of false positive results such as null evaluating as 0 is a bad idea. You should always be over-explicit. Quote Link to comment https://forums.phpfreaks.com/topic/265455-more-of-a-theory-question-on-uninitiated-variables/#findComment-1360476 Share on other sites More sharing options...
hakimserwa Posted July 10, 2012 Share Posted July 10, 2012 You can also use a while loop. Quote Link to comment https://forums.phpfreaks.com/topic/265455-more-of-a-theory-question-on-uninitiated-variables/#findComment-1360480 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.