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.

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
Cut PHP Code Review Time & Bugs into Half with CodeRabbit logo

Cut PHP Code Review Time & Bugs into Half with CodeRabbit

CodeRabbit is an AI-powered code review tool that specializes in PHP and Laravel, running PHPStan and offering automated PR analysis, security checks, and custom review features while remaining free for open-source projects.

Cut PHP Code Review Time & Bugs into Half with CodeRabbit
Join the Mastering Laravel community logo

Join the Mastering Laravel community

Connect with experienced developers in a friendly, noise-free environment. Get insights, share ideas, and find support for your coding challenges. Join us today and elevate your Laravel skills!

Join the Mastering Laravel community
Laravel Idea for PhpStorm logo

Laravel Idea for PhpStorm

Ultimate PhpStorm plugin for Laravel developers, delivering lightning-fast code completion, intelligent navigation, and powerful generation tools to supercharge productivity.

Laravel Idea for PhpStorm
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

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

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
Rector logo

Rector

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

Rector
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
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
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
Tim Leland: URL Shorteners, browser extensions, and more image

Tim Leland: URL Shorteners, browser extensions, and more

Read article
HTTP Method Verification in Laravel image

HTTP Method Verification in Laravel

Read article
Packistry is a Self-hosted Composer Repository Made with Laravel image

Packistry is a Self-hosted Composer Repository Made with Laravel

Read article
Rector v2.0 image

Rector v2.0

Read article
How to document multiple APIs in Laravel with Scramble image

How to document multiple APIs in Laravel with Scramble

Read article
Humanize String, Number, and Date Values into Readable Formats  image

Humanize String, Number, and Date Values into Readable Formats

Read article