PHP 数组示例为什么会留下一个逗号?

我见过下面这样的例子:

$data = array(
'username' => $user->getUsername(),
'userpass' => $user->getPassword(),
'email' => $user->getEmail(),
);

然而,在实践中我总是把 没有留在逗号后面。是我做错了什么,还是这只是“另一种”做事方式?如果我使用一个框架,后面的逗号不会对代码生成产生负面影响吗?我已经看到在其他语言(Java,C + +)的数组声明中使用尾随逗号,所以我假设留下尾随逗号的原因不是 PHP 特有的,但这引起了我的兴趣。

26484 次浏览

Why do PHP Array Examples Leave a Trailing Comma?

Because they can. :) The PHP Manual entry for array states:

Having a trailing comma after the last defined array entry, while unusual, is a valid syntax.

Seriously, this is entirely for convenience so you can easily add another element to the array without having to first add the trailing comma to the last entry.

Speaking of other languages: Be careful with this in JavaScript. Some older browsers will throw an error, though newer ones generally allow it.

I can't speak for other people, but I usually leave a trailing comma in my code. I do so because if/when I later add to the array, I do not have to worry about missing out a comma due to forgetting to add a comma to what was previously the last line.

I'm always doing trailing comma because it helps to avoid syntax errors while adding new array elements... it's just a good practice.

This is a good practice when defining array on multiple lines. It's also encouraged by ZendFramework's coding standards:

When using this latter declaration, we encourage using a trailing comma for the last item in the array; this minimizes the impact of adding new items on successive lines, and helps to ensure no parse errors occur due to a missing comma.

I feel that even though it is allowed it is bad practice, its like leaving out the last semi colon of your functions and loops.

Because it keeps entries uniform.

If you've had to swap the order, or add or delete entries, you know being able to leave a trailing comma is very convenient.

If the last element cannot have a comma, then you end up having to maintain the last comma by modifying entries. It's a pointless exercise and a waste of time and finger strokes because the intent of swapping or modifying entries is already accomplished.

By allowing a trailing comma on the last element, it frees the programmer from having to tend to this annoying and fruitless detail.

I noticed when working with version control (git) that if we add 1 thing to an array and we don't have the trailing comma, it will look like we modified 2 lines because the comma had to be added to the previous line. I find this looks bad and can be misleading when looking at the file changes, and for this reason I think a trailing comma is a good thing.

The reason is commit changes.

If you have to add the trailing comma when adding a new element. You're changing 1 line and adding 1 line. (-++)

When adding a new element when a comma is already in the line above. There is only 1 added line, and no changed ones. (+)

This surprised me recently, but it makes sense. I have long tried to adhere to an earlier convention that accomplishes the same thing, which is to put the separating comma in front of each entry rather than at the end.

$data = array(
'username' => $user->getUsername()
, 'userpass' => $user->getPassword()
, 'email' => $user->getEmail()
);

The commas also all line up that way, which looks nice, but it can make the indenting a little awkward. Maybe for that reason, it doesn't seem to have caught on much over the years, and I've had others ask me why I do it. I guess PHP's solution is a good compromise, and in any case, it's apparently the accepted solution now.

I've always added commas at the start of the new entry. Compilers see it as the single-character token of look-ahead that says "there is another one coming". I don't know if modern compilers use LR(1) (left-recursive, single token look-ahead) but I think that's where the syntax error originates when an a comma has nothing after it. It is rare that I've ever had another developer agree with me, but it looks like JohnBrooking does!

If you look at an example of roundcube file config (config.inc.php), they have example with and without trailing comma.

This array defines what plugins should be enabled or disabled:

...
// List of active plugins (in plugins/ directory)
$config['plugins'] = array(
'managesieve',
'password',
'archive',
'zipdownload',
);
...

Normally, this would be line by line and if somebody wants to add something on the array, they can do this:

...
// List of active plugins (in plugins/ directory)
$config['plugins'] = array(
'managesieve', //code by personA
'password', //code by personA
'archive', //code by personA
'zipdownload', //code by personA
'newplugin', //new code by personB
);
...

So, when they commit this code, they see only one changes for that particular line and this is more readable when inspecting who is making the code changes for that particular line.

In another line of code you can see this without trailing comma:

...
$config['default_folders'] = array('INBOX', 'Drafts', 'Sent', 'INBOX.spam', 'Trash');
...

Normally it would be a single line of code where nobody expects this code to be changed frequently.

In another word:

1) Put trailing comma if the array is used as an option or configuration file that might need to be changed dynamically in the future. Besides, if you make changes to that array programmatically using trailing comma you only make changes to one line code, whereas without it, you have to deal with 2 line of codes and this can cause more complexity to parse the array

2) You don't have to put trailing comma if the array is a constant array and you don't expect it to change in the future but as mentioned by the Accepted Answer, you can put trailing comma but it has no purpose