Edward
Edward

Reputation: 9778

When must you use array() to define an array?

In PHP I have seen the following types of coding:

my_array[] = "fish";
my_array[] = "book";
my_array[] = "chair";

Other times I have seen this done:

my_array = array();
my_array[] = "fish";
my_array[] = "book";
my_array[] = "chair";

What would be the purpose of "defining" an array in PHP with array() like above? Is it a good practice, if so why?

Upvotes: 0

Views: 47

Answers (2)

Yotam Omer
Yotam Omer

Reputation: 15356

You don't have to declare variables in PHP. this is just a better practice that improves readability and can prevent some errors on array using function you might use later on..

Upvotes: 0

John Conde
John Conde

Reputation: 219804

It is good practice to make sure your variable is defined before you use it.

Upvotes: 3

Related Questions