yretuta
yretuta

Reputation: 8091

how is array(0) different from array()

$variable = array(0);

$variable = array();

how are they different?

Upvotes: 1

Views: 293

Answers (4)

fresskoma
fresskoma

Reputation: 25781

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

Upvotes: 2

Paul Dixon
Paul Dixon

Reputation: 300975

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) {
}

Upvotes: 7

Pascal MARTIN
Pascal MARTIN

Reputation: 401132

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.

Upvotes: 4

meder omuraliev
meder omuraliev

Reputation: 186662

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

Upvotes: 9

Related Questions