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? Link to comment https://forums.phpfreaks.com/topic/28337-why-create-an-empty-array/ 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] Link to comment https://forums.phpfreaks.com/topic/28337-why-create-an-empty-array/#findComment-129607 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 Link to comment https://forums.phpfreaks.com/topic/28337-why-create-an-empty-array/#findComment-129608 Share on other sites More sharing options...
heavyEddie Posted November 24, 2006 Author Share Posted November 24, 2006 Thanks for the quick responses. Link to comment https://forums.phpfreaks.com/topic/28337-why-create-an-empty-array/#findComment-129617 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 Link to comment https://forums.phpfreaks.com/topic/28337-why-create-an-empty-array/#findComment-129619 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... Link to comment https://forums.phpfreaks.com/topic/28337-why-create-an-empty-array/#findComment-129635 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. Link to comment https://forums.phpfreaks.com/topic/28337-why-create-an-empty-array/#findComment-129636 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. Link to comment https://forums.phpfreaks.com/topic/28337-why-create-an-empty-array/#findComment-129691 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.