6

From my C++ knowledge base, I tend to initialize arrays in PHP by typing:
$foo = array()
Or I may bring this custom from Javascript, anyway, is this of any use?
As there's no problem in doing this:
$foo[45] = 'bar' without initializing it as an array, I guess not.

PS: the tags improvement is really good

Petruza
  • 11,744
  • 25
  • 84
  • 136

7 Answers7

6

Yes it is. At the very least in improves readability of code (so that you don't need to wonder 'where does $foo come from? Is it empty, or is there anything in it?`.

Also it will prevent 'Variable '$a' is not set notices, or Invalid argument passed to foreach in case you don't actually assign any values to array elements.

Mchl
  • 61,444
  • 9
  • 118
  • 120
  • +1 when I see something like `$foo[45] = 'bar'` the first thing I think of is "where is `$foo` initialized and what exactly is it?" – Gabi Purcaru Aug 11 '11 at 15:08
1

Either method is perfectly acceptable. As mentioned, this practice of using the array() construct is typically carried over from another language where you initialize before populating. With PHP, you can initialize an empty array and then populate later, or you can simply establish an array by assignments, such as $variableName[0] = "x";.

Robert
  • 8,717
  • 2
  • 27
  • 34
1

@petruz, that's the best way to do this, no only it will save you from nasty PHP error messages saying that function expects the parameter to be an array but, IMHO, this is the best way to write code. I initialise a variable before using it

Kumar
  • 5,038
  • 7
  • 39
  • 51
1

Initializing variables before use is good practice. Even if it is not required.

vaidas
  • 514
  • 3
  • 9
1

I've had problems (in older versions of PHP, haven't tried recently) where I was acting on array with array_push or something and PHP barked at me. As a general rule it's not necessary, but it can be safer, especially if you're dealing with legacy code; perhaps you're expecting $foo to be an array, but it's actually a boolean? Bad things ensue.

Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254
1

With initialization:

$myArray = array();
if ($myBoolean) {
  $myArray['foo'] = 'bar';
}
return $myArray;

Without initialization:

if ($myBoolean) {
  $myArray['foo'] = 'bar';
}
return $myArray;

In the first case it's clear what you want to happen if $myBoolean is false. In the second case it is not and php may throw a warning when you try and use $myArray later. Obviously this is a simplified case, but in a complex case the "if" may be a few lines down and/or not even exist until someone comes along and adds it later without realizing the array wasn't initialized.

While not necessary, I have seen lack of initialization cause non-obvious logic problems like this in complex functions that have been modified a lot over time.

Tim Gautier
  • 29,150
  • 5
  • 46
  • 53
1

It's good practice. Sooner or later you'll encounter a situation where you might want to do something like this:

array_push($foo, '45');

Which will throw a notice, whereas:

$foo = array();
array_push($foo, '45');

won't.

Evernoob
  • 5,551
  • 8
  • 37
  • 49
  • Right, I came across this with some array_ functions and foreach. But I guess that if PHP is such a loose language, it may as well consider null as an empty array and push the element to it anyway. – Petruza Aug 12 '11 at 14:32