in Php

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 :

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 ..

trail Twotrail One

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 :

  1. Grouped namepaces
  2. Function/method arguments (declarations & calls)
  3. Interface implementations on a class
  4. Trait implementations on a class
  5. Class member lists
  6. Inheriting variables from the parent scope in anonymous functions

Above list is taken from the PHP doc . Happy Coding 🙂