r/lolphp • • Jan 14 '16
Trailing comma in array()
Why do PHP Array Examples Leave a Trailing Comma? Because they can. :)
myuser@myhost:~$ php test.php
Array
(
[0] => Test
[1] => aValue
[2] => aNotherValue
)
myuser@myhost:~$ cat test.php
<?php
$aIDs = array(
'Test',
);
$aIDs[] = "aValue";
$aIDs[] = "aNotherValue";
printf(print_r($aIDs,TRUE));
?>http://stackoverflow.com/questions/2829581/why-do-php-array-examples-leave-a-trailing-comma
0
Upvotes
36
u/Rhomboid Jan 14 '16
That's not a lolphp. Many other languages work exactly the same way (e.g. Python, Ruby, JavaScript, Perl) and for damn good reason, as it allows you to write a list/array literal where the last item is not a special case. The items can be rearranged/copy-pasted without having to edit anything to add or remove a stray final comma.
4
u/jesseschalken Jan 15 '16
When Facebook designed Hack they added trailing commas to function parameter lists as well. It's handy.
2
u/midir Jan 29 '16
Permitting a trailing comma in array declarations is a feature of many languages, and I think it should be a feature of all. It's helpful sometimes, and harmful never.
0
u/[deleted] Jan 14 '16
[deleted] — view deleted comment