php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #50354 Allow trailing commas in function args
Submitted: 2009-12-01 16:25 UTC Modified: 2017-11-05 20:04 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:0 (0.0%)
From: jean dot marc dot leger at gmail dot com Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 5.2.11 OS: Gnu/Linux i686
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: jean dot marc dot leger at gmail dot com
New email:
PHP Version: OS:

 

 [2009-12-01 16:25 UTC] jean dot marc dot leger at gmail dot com
Description:
------------
PHP has always tolerated one trailing comma (,) at the end of array declaration, producing no error. I assume this is intended to easily generate array-syntax compliant php code in a loop.
<?php
/* Array context */
$a = array();          // works fine
// $a = array(,);      // will not work: Parse error: syntax error, unexpected ',', expecting ')'
$a = array(1,2);       // works fine
$a = array(1,2,);      // works fine : PHP syntax tolerance : last comma is ignored
//$a = array(1,2,3,,); // will not work: Parse error: syntax error, unexpected ',', expecting ')'
?>

It would be great to have PHP behaving the same way in function declaration and function call parameters contexts.

Reproduce code:
---------------
<?php
/* function declaration context */
function myfunction ()     {return true;}    // works
//function myfunction1 (,){return true;}     // will not work: Parse error: syntax error, unexpected ')', expecting '&' or T_VARIABLE
function myfunction2($a,$b){return true;}    // works
//function myfunction3($a,$b,){return true;} // will not work (should have): Parse error: syntax error, unexpected ')', expecting '&' or T_VARIABLE

/* function call context */
myfunction();           // works fine
//myfunction2(,);       // will not work: throws Parse error: syntax error, unexpected ',', expecting ')'
myfunction3($a,$a);     // works fine
//myfunction4($a,$a,);  // will not work (should have): throws Parse error: syntax error, unexpected ')'

Expected result:
----------------
<?php
// This declaration shouldn't raise any parse error
function myfunction3($a,$b,){return true;}
// neither does this function call :
myfunction4(1,2,);
// in either cases, the last comma should just be silently ignored by the parser.

Actual result:
--------------
<?php
function myfunction3($a,$b,){return true;} 
// Parse error: syntax error, unexpected ')', expecting '&' or T_VARIABLE

myfunction4($a,$a,);  
// Parse error: syntax error, unexpected ')'


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-12-01 18:57 UTC] jani@php.net
Trailing commas in function args do not exist in any language and will not ever exist in PHP either.
 [2017-11-05 20:04 UTC] jean dot marc dot leger at gmail dot com
-Package: Feature/Change Request +Package: *General Issues
 [2017-11-05 20:04 UTC] jean dot marc dot leger at gmail dot com
@jani any thought on this ? Perhaps you might want to change something to your previous answer. https://wiki.php.net/rfc/trailing-comma-function-calls
 [2017-11-05 20:27 UTC] spam2 at rhsoft dot net
your example has the trailing comma in the function DECLARATION which makes no sense and is also not what the rfc proposes
 [2017-11-05 20:41 UTC] jean dot marc dot leger at gmail dot com
@spam2 That is one point of this report. The other point, which you apparently missed, is just exactly what you see on the rfc. Though I fully agree with you, it makes a lot more sense in function call context than in function declaration context, and that is what I was really meaning to discuss here.(At the time, eight years ago, I supposed both topics were tightly linked to one another, so I went here looking for expert opinions and open-minded criticism). The rest, before your eyes, is history now.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Dec 08 18:00:01 2025 UTC