tags:

views:

215

answers:

4
$variable = array(0);

$variable = array();

how are they different?

+9  A: 

The first populates an array with a number 0, the latter is an empty array.

meder
Correct. And thus the first one has an element in it, while the second one does not (it is empty).
dave
+4  A: 

In the first case :

$variable = array(0);
var_dump($variable);

You get :

array
  0 => int 0

ie, an array with an element whose value is 0.


And, in the second case :

$variable = array();
var_dump($variable);

you get :

array
  empty

ie, an empty array.

Pascal MARTIN
+7  A: 

The first contains a single element, a integer zero. The parameter is not a "size initializer" as you might imagine. You can see this by using var_dump on them:

$foo = array(0);
var_dump($foo);

$bar = array();
var_dump($bar);

This outputs

array(1) {
  [0]=>
  int(0)
}
array(0) {
}
Paul Dixon
+2  A: 

In addition to meder:

$variable = array(0);
count($variable); // 1
empty($variable); // false
(!$variable)  // false

$variable = array();
count($variable); // 0
empty($variable); // true
(!$variable)  // true
x3ro