0

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?

4
  • my_array is an array (PHP is intelligent to detect its type), You are pushing elements to that array. Commented Jul 6, 2013 at 4:56
  • did you mean my_array = array();? Commented Jul 6, 2013 at 4:57
  • @Musa Yes, typo. It's been corrected. Thanks for pointing it out! Commented Jul 6, 2013 at 5:04
  • look into weak vs strong typing. Haters hate the php for this kind of behavior. Commented Jul 6, 2013 at 5:24

2 Answers 2

3

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

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

Comments

0

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..

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.