为什么 PHP 数组示例要留下尾随逗号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2829581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do PHP Array Examples Leave a Trailing Comma?
提问by ashurexm
I have seen examples like the following:
我见过如下例子:
$data = array(
'username' => $user->getUsername(),
'userpass' => $user->getPassword(),
'email' => $user->getEmail(),
);
However, in practice I have always notleft the trailing comma. Am I doing something wrong, or is this just 'another' way of doing it? If I was using a framework would not having the trailing comma affect code generation negatively? I have seen the use of trailing commas in array declarations in other languages (Java, C++) as well, so I assume the reasons for leaving trailing commas are not specific to PHP, but this has piqued my interest.
但是,在实践中,我始终没有留下尾随逗号。我做错了什么,还是这只是“另一种”做事方式?如果我使用的是框架,尾随逗号不会对代码生成产生负面影响吗?我也看到过在其他语言(Java、C++)的数组声明中使用尾随逗号,所以我认为保留尾随逗号的原因并非特定于 PHP,但这激起了我的兴趣。
回答by Pekka
Why do PHP Array Examples Leave a Trailing Comma?
为什么 PHP 数组示例要留下尾随逗号?
Because they can. :) The PHP Manual entry for arraystates:
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. Firefox will leniently tolerate trailing commas; Internet Explorer will, rightly, throw an error.
说到其他语言:在 JavaScript 中要小心这一点。Firefox 会宽容地容忍尾随逗号;Internet Explorer 会正确地抛出错误。
回答by Janci
This is a good practice when defining array on multiple lines. It's also encouraged by ZendFramework's coding standards:
在多行上定义数组时,这是一个很好的做法。ZendFramework 的编码标准也鼓励它:
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.
使用后一个声明时,我们鼓励对数组中的最后一项使用尾随逗号;这最大限度地减少了在连续行上添加新项目的影响,并有助于确保不会由于缺少逗号而发生解析错误。
回答by user985366
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.
我在使用版本控制 (git) 时注意到,如果我们向数组中添加 1 个东西并且我们没有尾随逗号,看起来我们修改了 2 行,因为必须将逗号添加到前一行。我发现这看起来很糟糕,并且在查看文件更改时可能会产生误导,因此我认为尾随逗号是一件好事。
回答by ahnbizcad
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.
通过在最后一个元素上允许一个尾随逗号,它使程序员不必处理这个烦人且毫无结果的细节。
回答by Yani
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. (-++)
如果在添加新元素时必须添加尾随逗号。您正在更改 1 行并添加 1 行。(-++)
When adding a new element when a comma is already in the line above. There is only 1 added line, and no changed ones. (+)
当逗号已经在上面的行中时添加新元素。仅添加了 1 行,没有更改的行。(+)
回答by Peter Horne
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.
我不能代表其他人说话,但我通常会在代码中留下逗号。我这样做是因为如果/当我以后添加到数组时,我不必担心由于忘记在以前的最后一行添加逗号而错过逗号。
回答by Skelly1983
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.
我觉得即使允许它也是不好的做法,就像省略了函数和循环的最后一个分号。
回答by Kirzilla
I'm always doing trailing comma because it helps to avoid syntax errors while adding new array elements... it's just a good practice.
我总是做尾随逗号,因为它有助于在添加新数组元素时避免语法错误......这只是一个很好的做法。
回答by JohnBrooking
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 frontof 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.
逗号也都是这样排列的,这看起来不错,但它会使缩进有点尴尬。也许正是因为这个原因,这些年来它似乎并没有流行起来,而且我也有其他人问我为什么要这样做。我想 PHP 的解决方案是一个很好的折衷方案,无论如何,它现在显然是公认的解决方案。
回答by Bill Hedge
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!
我总是在新条目的开头添加逗号。编译器将其视为前瞻的单字符标记,表示“还有另一个即将到来”。我不知道现代编译器是否使用 LR(1)(左递归,单标记前瞻),但我认为当逗号后面没有任何内容时,这就是语法错误的来源。很少有其他开发人员同意我的看法,但看起来像 JohnBrooking 一样!

