PHP数组的例子,为什么会留下拖逗号? [英] Why do PHP Array Examples Leave a Trailing Comma?

查看:184
本文介绍了PHP数组的例子,为什么会留下拖逗号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经见过像下面的例子:

  $数据=阵列(
   用户名= GT; $用户可与GT; getUsername(),
   为userpass'=> $用户可> getPassword来()
   电子邮件=> $用户可与GT; getEmail(),
);

然而,在实践中我一直的不可以离开了后面的逗号。我做得不对,或者是做这个只是别人的方式吗?如果我是使用框架不会有后面的逗号产生负面影响code一代?我已经看到了使用其他语言(Java,C ++),以及尾随数组声明逗号,所以我想离开尾随逗号的原因并不是专门针对PHP,但是这激起了​​我的兴趣。


解决方案

  

为什么PHP阵列的例子留下拖逗号?


由于他们可以。 :)数组国 PHP手册条目:


  

到了最后一个定义的数组条目之后尾随逗号,而不同寻常,是一种有效的语法。


说真的,这完全是为了方便让您可以​​轻松地添加其他元素的数组,而无需先后面的逗号添加到最后一个条目。

其他语言的演讲:要小心这在JavaScript中。 Firefox将从轻容忍尾随逗号; Internet Explorer将正确地,抛出一个错误。

I have seen examples like the following:

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

However, in practice I have always not left 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.

解决方案

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. Firefox will leniently tolerate trailing commas; Internet Explorer will, rightly, throw an error.

这篇关于PHP数组的例子,为什么会留下拖逗号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆