in what cases in arrays on php at the end is a comma set and in which not? Or is it always put ??
- Some variants of php sniffer (for WordPress Extra, say) require a comma at the end of each element of the array. - KAGG Design
|
2 answers
The last comma is usually placed for convenience, in case the elements are placed on separate lines. Without a comma, when adding a new element of the array, version control systems would record a change of two lines (a comma appeared in one line, and a new one in a new line), and not one. In addition, adding a new element can be conveniently duplicating the previous one (in the code editors there are special shortcut keys for duplicating a line). If the array elements are on the same line, the last comma is usually not put.
$arr1 = [ 'foo' => 'Foo', 'bar' => 'Bar', ]; $arr2 = [1, 2]; |
At your discretion. The syntax allows both.
|