Get expert guidance in a few days with a Laravel code review

PHP 7.3: Trailing Commas in Function Calls

Published on by

PHP 7.3: Trailing Commas in Function Calls image

Well PHP 7.3 won’t have arrow functions (that would be dreamy). However, trailing commas in function calls is an excellent addition coming to PHP 7.3.

In PHP 7.3, trailing commas in function calls will be valid syntax. That is to say, you can use trailing commas when calling functions, but not defining them.

Trailing commas in function calls are specifically useful for variadic functions:

//
unset(
$one,
$two,
$three,
);
 
return view('posts.show', compact(
'post',
'author',
'comments',
));

Importantly, this change only affects call syntax, not function declaration! The following syntax is still illegal in PHP 7.3:

// Still illegal syntax
public function __construct(
$apiClient,
array $options = [],
) {
 
}

Trailing Commas in PHP Arrays

Using trailing (sometimes referred to as dangling) commas is nice and clean when you want to rearrange an array or append new values.

The trailing comma in PHP arrays has been legal forever (I am not sure when they were introduced) and might be your only exposure to trailing commas in PHP apart from making it legal in grouped namespaces since PHP 7.2.

One argument of why trailing commas are beneficial, and why some teams have been enforcing this code style in arrays, is cleaner diffs. When you want to append a new array item, when you add a comma to the last line to append another value, the diff now affects both lines:

// Before
$points = [
1,
5,
7
];
 
// After
$points = [
1,
5,
+ 7,
+ 9
];

With the trailing comma, only the appended line shows up in the diff, nice an clean!

// Ahh better
$points = [
1,
5,
7,
+ 9,
];

Trailing Commas in Other languages

JavaScript

Trailing commas in JavaScript array literals have been allowed since the beginning. Starting in ECMAScript 5, trailing commas in object literals are legal, and in ECMAScript 2017 trailing commas in function parameter lists are legal:

var numbers = [
1,
2,
3,
];
 
// ECMAScript 5
var stats = {
rebounds: 9,
fouls: 2,
points: 15,
};
 
// ECMAScript 2017
function track(
eventName,
eventValue,
) {
// ...
}

In JavaScript trailing commas are also allowed in destructuring assignments:

var {
rebounds,
points,
assists,
} = stats;

Ruby

Here’s an example of a trailing comma on a hash and an array:

ruby_hash = {
first: '1st',
second: '2nd',
third: '3rd',
}
 
ruby_array = [
'1st',
'2nd',
'3rd',
]

Python

The Python examples look identical to ruby:

countries = [
'China',
'India',
'Mexico',
]

An interesting tidbit, if you were to omit the trailing comma, and accidentally add another item to the array, here’s what the result would be:

countries = [
'China',
'India'
]
 
// Later you added a new item but forgot the comma
countries = [
'China',
'India'
'Mexico'
]
 
// The result would be...
['China', 'IndiaMexico']

Haskell

Haskell is known for its style of the “trailing comma” called the “Haskell style”:

data Settings = -- The user settings
{ has_sound :: Bool
, has_power :: Bool
, has_graphics :: Bool
, user_name :: String
, user_password :: String
, user_email :: Email
, stylesheet :: Style
}

The example comes from this StackOverflow answer; and no, I don’t know Haskell.

Because trailing commas can cause cross-browser issues in JavaScript, you might have seen a similar style to this in JavaScript as well:

var stats =
{ rebounds: 9
, fouls: 2
, points: 15
};

Learn More

You can learn all about the most prominent features coming to PHP 7.3 in our post announcing the PHP 7.3 Alpha 1 release last week. PHP 7.3 will hit General availability (GA) later this year!

Also, read through the RFC for allowing a trailing comma in function calls for full details on the proposal coming to PHP 7.3.

The author Sammy Kaye Powers did an excellent job conveying the proposal. If you read carefully, you can learn about how this RFC communicates the difference between this 7.3 proposal and a previously rejected 7.2 version. The 7.2 RFC vote combined both function calls and definitions in a single vote and was narrowly rejected.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Filed in:
Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit

The latest

View all →
PHP Debugger: A Lightweight Xdebug Alternative Built for Speed image

PHP Debugger: A Lightweight Xdebug Alternative Built for Speed

Read article
Laravel USPS: A Modern Wrapper for the USPS API image

Laravel USPS: A Modern Wrapper for the USPS API

Read article
Debugbar releases v4.2.0 and add a new Boost skill image

Debugbar releases v4.2.0 and add a new Boost skill

Read article
New Expressive Model Attributes in Laravel 13.2.0 image

New Expressive Model Attributes in Laravel 13.2.0

Read article
Inertia.js v3.0.0 Is Here with Optimistic Updates, useHttp, and More image

Inertia.js v3.0.0 Is Here with Optimistic Updates, useHttp, and More

Read article
Laravel Boost v2.4.0 Adds Security Audits and a Laravel Best Practices Skill image

Laravel Boost v2.4.0 Adds Security Audits and a Laravel Best Practices Skill

Read article