Trailing comma after the last array entry seems quite unusual, while is a valid syntax . It has been with us for a long time. This make easy to append new elements to a list .
Let’s take a look at a simple example :
$example = [
Trailing commas looks unusual in the given example . I have coded without trailing commas for years but let’s take a look at the advantages that we can have over arrays or any other lists without trailing commas . One of them is diff . Very useful when using Version control system like git, cvs, svn etc ..
From the above image, we can see clean visual diff when using trailing commas . Using trailing commas , we are simply appending new items rather amending .
git diff : Show changes between commits, commit and working tree, etc
Currently , trailing commas are supported in the following lists :
Function/method arguments (declarations & calls)
Interface implementations on a class
Trait implementations on a class
Class member lists
Inheriting variables from the parent scope in anonymous functions
Above list is taken from the PHP doc . Happy Coding 🙂