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
Tinkerwell

Version 4 of Tinkerwell is available now. Get the most popular PHP scratchpad with all its new features and simplify your development workflow today.

Visit Tinkerwell
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
LoadForge logo

LoadForge

Easy, affordable load testing and stress tests for websites, APIs and databases.

LoadForge
Paragraph logo

Paragraph

Manage your Laravel app as if it was a CMS – edit any text on any page or in any email without touching Blade or language files.

Paragraph
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
Bacancy - Staff Augmentation logo

Bacancy - Staff Augmentation

Leave your web app development hustles to the leading IT Staff Augmentation Service Providers. Choose from an extensive pool of 1050+ developers and give yourself the sigh of success you deserve with Bacancy. Get In Touch Today!

Bacancy - Staff Augmentation
DocuWriter.ai logo

DocuWriter.ai

Save hours of manually writing Code Documentation, Comments & DocBlocks, Test suites and Refactoring.

DocuWriter.ai
Rector logo

Rector

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

Rector

The latest

View all →
Add Architecture Tests to Saloon API Integrations with Lawman image

Add Architecture Tests to Saloon API Integrations with Lawman

Read article
Protect Routes with JWT Tokens Using This Package for Laravel image

Protect Routes with JWT Tokens Using This Package for Laravel

Read article
Increment a Rate Limiter by a Custom Amount in Laravel 10.46 image

Increment a Rate Limiter by a Custom Amount in Laravel 10.46

Read article
Add Kanban Boards to Your Laravel App in Seconds image

Add Kanban Boards to Your Laravel App in Seconds

Read article
October CMS v3.6 Ships Today, Full of New Features image

October CMS v3.6 Ships Today, Full of New Features

Read article
Laracon EU Videos are now out image

Laracon EU Videos are now out

Read article