Trailing Commas Everywhere
PHP progressively allowed trailing commas in more places: arrays (always), function calls (7.3), parameter lists (8.0), and closure use lists (8.0). Clean diffs, easy reordering, no syntax errors when adding items.
// Arrays (always supported — PHP was ahead of JS here!)
$config = [
'debug' => true,
'cache' => false,
'log_level' => 'info', // ← trailing comma, always worked
];
// Function/method calls (PHP 7.3)
$result = sprintf(
'%s has %d items worth $%.2f',
$name,
$count,
$total, // ← no more removing this comma when adding a line below
);
// Function/method declarations (PHP 8.0)
function createUser(
string $name,
string $email,
Role $role = Role::User, // ← add new params without touching this line
) {
// ...
}
// Closure use lists (PHP 8.0)
$fn = function () use (
$config,
$logger,
$cache, // ← consistent everywhere
) {
// ...
};
Trailing commas mean cleaner git diffs: adding a new item only shows one changed line, not two (the new line plus the comma added to the previous line). They also make reordering lines trivial — no comma juggling.
Significance: Developer Experience
Trailing commas are a small syntax feature with outsized impact on daily workflow. They eliminate an entire class of syntax errors, produce cleaner version control diffs, and make copy-pasting and reordering lines effortless. It's the kind of thoughtful quality-of-life improvement that shows PHP listens to its developers.