heavyEddie Posted November 24, 2006 Share Posted November 24, 2006 I've been working with PHP for about 9 months now. I'm starting to look inside many other scripts and comparing to my own. I've noticed many times folks will create an empty array. Why? Does this increase speed? Quote Link to comment Share on other sites More sharing options...
CheesierAngel Posted November 24, 2006 Share Posted November 24, 2006 It does not increase speed but let the programmer now in advance that the variable will be an array.Once it is initialized as an array you can easly add new element to the array.[code]<?php// Initialize empty array$vars = array();// Add elements to the arraywhile ($counter < $maxCounter) { $vars[] = "This is element: $counter";}?>[/code] Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 24, 2006 Share Posted November 24, 2006 I use it to make sure I know that any previous contents of an array have been cleared, especially when I'm using temporary arrays.Ken Quote Link to comment Share on other sites More sharing options...
heavyEddie Posted November 24, 2006 Author Share Posted November 24, 2006 Thanks for the quick responses. Quote Link to comment Share on other sites More sharing options...
printf Posted November 24, 2006 Share Posted November 24, 2006 You should always initialize it before you try to fill it. Sure you can set error reporting to not display the [b]undefined notice[/b], but you should always initialize any array() that might be used. It just good programming logic.printf Quote Link to comment Share on other sites More sharing options...
taith Posted November 24, 2006 Share Posted November 24, 2006 $vars = array(); is completly unnessecary as [code]$vars[] = '';[/code]works just the same without being told to be an array... its only used as a placeholder for programmers... however, if the $vars is already a string, it will give errors... Quote Link to comment Share on other sites More sharing options...
Jenk Posted November 24, 2006 Share Posted November 24, 2006 [quote author=taith link=topic=116159.msg473141#msg473141 date=1164384746]$vars = array(); is completly unnessecary as [code]$vars[] = '';[/code]works just the same without being told to be an array... its only used as a placeholder for programmers... however, if the $vars is already a string, it will give errors...[/quote]It will give an error just as described in the post above yours, so please stop giving incorrect information. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted November 24, 2006 Share Posted November 24, 2006 [quote author=taith link=topic=116159.msg473141#msg473141 date=1164384746]$vars = array(); is completly unnessecary as [code]$vars[] = '';[/code]works just the same without being told to be an array... its only used as a placeholder for programmers... however, if the $vars is already a string, it will give errors...[/quote]$var = array(); and $var[] = ''; is not the same. The latter creates an array with an emtpy item. Where as $var = array(); creates just an empty array. I agree too it is good programming practice to initiate your variables first before filling them. Quote Link to comment 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.