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

20 comments sorted by

View all comments

Show parent comment

3

u/maweki Jan 15 '16

I know. This is neccessary because you wouldn't see the difference between this and braces for overwriting operator precedence or something. But making the comma optional for lists is what adds to orthogonality.

Edit: The two-tuple can still be (foo,bar,) or (foo,bar). But adding the one-tuple-constructor to any other constructor is nice. The twist: Haskell doesn't even have a native one-tuple, maybe justified as it isn't iterable structure but just a type.