NotionCommotion Posted October 1, 2017 Share Posted October 1, 2017 As long as I am certain that $arr hasn't been previously assigned and that I am not concerned attempting to use it and getting an unset warning, is there any reason to initiate arrays before using them? <?php //$arr=[]; foreach([[2,3,1],[0,4,2],[1,4,3],[2,4,4]] as $e) { //if(!isset($arr[$e[0]])) $arr[$e[0]]=[]; $arr[$e[0]][$e[1]]=$e[2]; } var_dump($arr); Quote Link to comment https://forums.phpfreaks.com/topic/305172-should-arrays-be-initialized/ Share on other sites More sharing options...
benanamen Posted October 1, 2017 Share Posted October 1, 2017 (edited) Some info here https://bugs.php.net/bug.php?id=28151 Edited October 1, 2017 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/305172-should-arrays-be-initialized/#findComment-1552213 Share on other sites More sharing options...
NotionCommotion Posted October 1, 2017 Author Share Posted October 1, 2017 Some info here https://bugs.php.net/bug.php?id=28151 Meaning? Quote Link to comment https://forums.phpfreaks.com/topic/305172-should-arrays-be-initialized/#findComment-1552214 Share on other sites More sharing options...
Barand Posted October 1, 2017 Share Posted October 1, 2017 Meaning if you read the information on that page it will answer your question. Quote Link to comment https://forums.phpfreaks.com/topic/305172-should-arrays-be-initialized/#findComment-1552215 Share on other sites More sharing options...
NotionCommotion Posted October 1, 2017 Author Share Posted October 1, 2017 Meaning if you read the information on that page it will answer your question. I interpret that obscure post to be that it is acceptable, however, I don't feel it is an authoritative source and it might be outdated being over 13 years old. And while attempting to do so does not create an error, that does not mean one should do so. According to http://php.net/manual/en/language.types.array.php, it is discouraged as it might be previously set as a string before doing so. Quote Link to comment https://forums.phpfreaks.com/topic/305172-should-arrays-be-initialized/#findComment-1552217 Share on other sites More sharing options...
kicken Posted October 2, 2017 Share Posted October 2, 2017 I'd say it comes down more to your preference. I like to initialize my variables (any, not just array) if their value is generated through a loop, branching statements, etc. I find it makes the code more readable. If you're following good practices such as meaningful variable names and separating code into various functions/classes then you shouldn't have problems with your array variable being previously defined as some other type. 1 Quote Link to comment https://forums.phpfreaks.com/topic/305172-should-arrays-be-initialized/#findComment-1552220 Share on other sites More sharing options...
phpmillion Posted October 2, 2017 Share Posted October 2, 2017 NotionCommotion, it depends... If you don't initialize arrays, in most of situations it will be fine even if you set error_reporting to E_ALL. However, you might encounter situations when it will produce undefined variable error; therefore, I would recommend initializing arrays. This way, you are sure that code doesn't raise any errors on different server environments. Quote Link to comment https://forums.phpfreaks.com/topic/305172-should-arrays-be-initialized/#findComment-1552225 Share on other sites More sharing options...
NotionCommotion Posted October 2, 2017 Author Share Posted October 2, 2017 Thanks Kicken and Phpmillion, I will stick to doing so when generated through a loop/branch statement/etc. Quote Link to comment https://forums.phpfreaks.com/topic/305172-should-arrays-be-initialized/#findComment-1552228 Share on other sites More sharing options...
kicken Posted October 2, 2017 Share Posted October 2, 2017 To kind of clarify what I was saying, in your specific example above, I would keep the$ar=[] line above the loop. The if statement inside the loop is unnecessary and I would drop it. Quote Link to comment https://forums.phpfreaks.com/topic/305172-should-arrays-be-initialized/#findComment-1552239 Share on other sites More sharing options...
NotionCommotion Posted October 2, 2017 Author Share Posted October 2, 2017 Thanks Kicken, I understood you the first time, but appreciate the confirmation. Quote Link to comment https://forums.phpfreaks.com/topic/305172-should-arrays-be-initialized/#findComment-1552256 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.