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 Forge

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

Visit Laravel Forge
Laravel Forge logo

Laravel Forge

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

Laravel Forge
Tinkerwell logo

Tinkerwell

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

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
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
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. We ♥ Laravel

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
All Green logo

All Green

All Green is a SaaS test runner that can execute your whole Laravel test suite in mere seconds so that you don't get blocked – you get feedback almost instantly and you can deploy to production very quickly.

All Green
Larafast: Laravel SaaS Starter Kit logo

Larafast: Laravel SaaS Starter Kit

Larafast is a Laravel SaaS Starter Kit with ready-to-go features for Payments, Auth, Admin, Blog, SEO, and beautiful themes. Available with VILT and TALL stacks.

Larafast: Laravel SaaS Starter Kit
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a 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
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector

The latest

View all →
Property Hooks Get Closer to Becoming a Reality in PHP 8.4 image

Property Hooks Get Closer to Becoming a Reality in PHP 8.4

Read article
Reversible Form Prompts and a New Exceptions Facade in Laravel 11.4 image

Reversible Form Prompts and a New Exceptions Facade in Laravel 11.4

Read article
Basset is an alternative way to load CSS & JS assets image

Basset is an alternative way to load CSS & JS assets

Read article
Integrate Laravel with Stripe Connect Using This Package image

Integrate Laravel with Stripe Connect Using This Package

Read article
The Random package generates cryptographically secure random values image

The Random package generates cryptographically secure random values

Read article
Automatic Blade Formatting on Save in PhpStorm image

Automatic Blade Formatting on Save in PhpStorm

Read article