1
$variable = array(0);

$variable = array();

how are they different?

1
  • total noob here...how do I do that? Commented Sep 22, 2009 at 1:06

4 Answers 4

9

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

Sign up to request clarification or add additional context in comments.

1 Comment

Correct. And thus the first one has an element in it, while the second one does not (it is empty).
7

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

Comments

4

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.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.