tags:

views:

85

answers:

2

Hi, Sometimes I see arrays like the following:


array('item1' => array(
         'subitem1',
         'subitem2',
      )

Why a comma is added at end of array wheras there is not any element after submitem2?

+8  A: 

It makes it easier to append another entry at a later point in time.

Oli Charlesworth
As a side-effect, wth version control software, the last line does not appear to be changed by adding a comma.
André Caron
@André CaronIt's a good reason to use it
Hamid.P
@Hamid: It is the *only* reason I use it :-)
André Caron
Sorry, I didn't get it.. could anyone explain it in a little more detail? How and why does it make it easier? thnx..
Lucius
If I want to e.g. rearrange items in the array declaration, or duplicate them, or copy-and-paste from another declaration, or auto-generate the source code, it's much easier if one doesn't have to worry about a special-case for the last entry.
Oli Charlesworth
oh, ok, now I see.. d'oh!Thanks!!
Lucius
A: 

The comma is not needed at all. But you will still find some interpreters/compilers still allowing to use it. You can think of it this way: the interpreters is allowing you to make little mistakes and wisely fixing it internally. This is just a user-friendly way. Nothing else at all.

And you should not use that extra comma because a lot of strict ones will not let you pass through! Like Internet Explorer while interpreting Javascript. But Firefox will allow it.

Muktadir
One problem with PHP is that there's no formal grammar specification, just the *de facto* reference implementation.
Oli Charlesworth
yes...there are more languages...like javascript...so it's better to follow a good pattern! Don't know why I got a negative feedback here. Probably I am too confusing on expressing theories :p
Muktadir