Jump to content

Why create an empty array?


heavyEddie

Recommended Posts

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 array
while ($counter < $maxCounter) {
$vars[] = "This is element: $counter";
}
?>
[/code]
[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 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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.