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]
Link to comment
Share on other sites

$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
Share on other sites

[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
Share on other sites

[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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.