0
$a[]="Anna";
$a[]="Brittany";
$a[]="Cinderella";
$a[]="Diana";
$a[]="Eva";
$a[]="Fiona";
$a[]="Gunda";

When declaring it this way, does it automatically increase the index? Why is this ideal?

6
  • What happens when you run that code and try to access the array? Commented May 17, 2010 at 5:23
  • 2
    It's ideal because it makes adding new elements to the array a breeze! You don't have to deal with incrementing the index. Commented May 17, 2010 at 5:27
  • 2
    you can also do the same thing as: $a = array('Anna', 'Brittany' ... ) Commented May 17, 2010 at 5:27
  • 1
    It is also useful when populating arrays using loops.. :D Commented May 17, 2010 at 5:31
  • @webdestroya I would pick your comment as best answer. Commented May 17, 2010 at 5:57

5 Answers 5

5

Yes it does. It may not be ideal for you, but it sure is convenient for most people.

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

Comments

1

Yes, it does. See the PHP arrays guide. Note that if you want to delete the array, you can use:

unset($a);

Or if you want to make $a become an empty array, you can use:

$a = array();

1 Comment

+1 for actually linking to the docs. Seriously, people.
0

Yes, it does. It may not be 'ideal' - but considering that you're setting an array equal to a single value, adding to the array is better than overwriting the entire array, which I suppose would be the other option for that statement.

Comments

0

Yes, It automatically increasing a index but start with '0'.

Comments

0

Yes its ideal, because if its not, then use an associative array.

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.