Take the State of PHP 2026 Survey →

Allow a trailing comma in function calls View as Markdown

Implemented (PHP 7.3)
Authors
Sammy Kaye Powers, me@sammyk.me
Date
2017-10-07
Version
0.1
Source
wiki.php.net/rfc/trailing-comma-function-calls

Vote

Allow a trailing comma in function calls closed
Yes
30
No
10
40 voters
ashnazg Yes 2017-10-23 16:56
bishop Yes 2017-10-30 00:32
bmajdak No 2017-10-28 15:21
bwoebi No 2017-10-21 20:54
colinodell Yes 2017-10-22 01:32
danack Yes 2017-10-21 18:52
daverandom Yes 2017-10-28 08:27
derick No 2017-10-22 22:31
dm No 2017-10-26 15:16
emir Yes 2017-10-24 19:09
fmargaine Yes 2017-10-21 19:04
francois Yes 2017-10-22 16:58
galvao Yes 2017-10-24 03:45
heiglandreas Yes 2017-10-24 15:42
jhdxr Yes 2017-10-22 14:09
kalle Yes 2017-10-21 19:06
kelunik Yes 2017-10-22 09:30
laruence No 2017-10-21 23:49
lbarnaud Yes 2017-10-22 20:11
lcobucci Yes 2017-10-22 19:11
leigh No 2017-10-24 10:47
leszek Yes 2017-10-23 08:35
levim No 2017-10-24 23:24
lstrojny Yes 2017-10-25 15:04
mariano Yes 2017-10-21 18:55
mcmic Yes 2017-10-23 07:16
mfonda Yes 2017-10-22 23:32
nikic Yes 2017-10-23 16:09
ocramius Yes 2017-10-22 11:58
peehaa No 2017-10-22 12:52
pmmaga No 2017-10-27 11:34
pollita Yes 2017-10-21 18:45
ramsey Yes 2017-10-28 13:54
rogeriopradoj Yes 2017-11-02 14:27
salathe No 2017-10-24 15:22
sammyk Yes 2017-11-04 15:31
stas Yes 2017-10-21 21:16
trowski Yes 2017-10-24 16:16
weierophinney Yes 2017-10-21 19:42
yunosh Yes 2017-10-23 07:06

Discussions on the mailing list

[RFC] [Discussion] Allow a trailing comma in function calls
by Sammy Kaye Powers · 5 messages
[VOTE] Allow a trailing comma in function calls
by Sammy Kaye Powers · 4 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC

Introduction

Allowing a trailing comma in function calls will make it more convenient to append arguments in many contexts where it is common to call a function with lots of arguments; especially variadic functions.

Proposal

A trailing comma has been allowed in array syntax since forever-ever, and in grouped namespace syntax since PHP 7.2.

# Currently possible
use Foo\Bar\{
    Foo,
    Bar,
};
 
$foo = [
    'foo',
    'bar',
];

Allowing a trailing comma makes sense in these contexts as new values get appended frequently. There is another context wherein appending more values frequently happens: calling a variadic function.

unset(
    $foo,
    $bar,
    $baz,
);

Frequent Context Examples

A trailing comma in function calls make it crazy convient to invoke any function that takes variable arguments (i.e. using the splat operator ... or func_get_args().)

Unsetting variables

It is extremely common to send a list of two or more arguments to unset() to unset variables.

unset(
    $somethingIDontNeedAnymore,
    $anotherOneToKill,
    $letsMakeThisEasy,
);

Sending variables to a template engine

Another extremely common practice is to send a list of variables to a template engine concisely using compact().

echo $twig->render(
    'index.html',
    compact(
        'title',
        'body',
        'comments',
    )
);

Merging arrays

Invoking array_merge() is another great example of how a trailing comma makes values easier to append.

$newArray = array_merge(
    $arrayOne,
    $arrayTwo,
    ['foo', 'bar'],
);

Debugging all the things

When you're quickly debugging with var_dump(), it's nice to not have to worry about removing that dangling comma in order for your script to run.

var_dump(
    $whatIsInThere,
    $probablyABugInThisOne,
    $oneMoreToCheck,
);

i18n & l10n

Internationalization & localization often makes use of variadic functions such as sprintf() that usually expand and contract over time.

$en = 'A trailing %s makes %s a happy developer.';
$text = sprintf(
    $en,
    'comma',
    'Jane',
);

And so on...

This list of examples is not meant to be exhaustive, but you can see how allowing a trailing comma in function calls fits well within the existing trailing comma contexts (arrays & grouped namespaces).

Method & closure calls too

Method calls also adopt trailing comma functionality.

class Foo
{
  public function __construct(...$args) {
    //
  }
 
  public function bar(...$args) {
    //
  }
 
  public function __invoke(...$args) {
    //
  }
}
 
$foo = new Foo(
  'constructor',
  'bar',
);
 
$foo->bar(
  'method',
  'bar',
);
 
$foo(
  'invoke',
  'bar',
);

And closures too.

$bar = function(...$args) {
  //
};
 
$bar(
  'closure',
  'bar',
);

"Not really a function" functions as well

There are two language constructs that look like functions but aren't that will also allow a trailing comma: unset() (as mentioned before) and isset().

unset($foo, $bar,);
var_dump(isset($foo, $bar,));

Wait, didn't we just vote on this?

Yes, there was an RFC to add trailing commas to all list syntax in PHP 7.2. Unfortunately due to an oversight on my end, the vote for function calls and function declarations was combined into one vote so the vote failed (but just barely!)

I was contacted by many “no” voters saying that they would have voted “yes” for function calls, but “no” for function declarations. This RFC proposes allowing a trailing comma in function call syntax only.

We are allowed to put this feature up for vote again since the mandatory 6-month waiting period has passed since the last vote and this RFC targets a new major version of PHP.

Backward Incompatible Changes

None

Proposed PHP Version(s)

PHP 7.3

What's not allowed

Function declaration syntax will not change. This RFC targets function call syntax only.

# Parse error
function bar($a, $b,) {
    //
}

Free-standing commas are not allowed.

# Parse error
foo(,);

Multiple trailing commas & leading commas are not allowed.

# Parse error
foo('function', 'bar',,);
# Also parse error
foo(, 'function', 'bar');

Other things that could look like functions like yield & list() will go untouched and no other list syntax will be affected.

Proposed Voting Choices

Requires a 2/3 majority to pass. Voting begins 2017-10-21 18:30 UTC and ends 2017-11-04 18:30 UTC.

Vote: Allow a trailing comma in function calls — Yes: 30 · No: 10 (closed)

Patches and Tests

This patch, sans the tests, is a trivial three-line change in the parser.

See the diff on GitHub

The trailing comma is thrown out at the parser level so there's no runtime computations wasted.

Implementation

Implemented in PHP 7.3 via https://github.com/php/php-src/commit/b591c329ee3129adbdc35141bb1542d119f7a2a1.