Skip to content

Commit 843d379

Browse files
authored
Merge pull request #18 from Crell/trailing-comma
Add a section on trailing commas
2 parents ab177f2 + b0bb59b commit 843d379

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

spec.md

+41
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,47 @@ Any new types and keywords added to future PHP versions MUST be in lower case.
117117
Short form of type keywords MUST be used i.e. `bool` instead of `boolean`,
118118
`int` instead of `integer` etc.
119119

120+
### 2.6 Trailing commas
121+
122+
Numerous PHP constructs allow a sequence of values to be separated by a comma,
123+
and the final item may have an optional comma. Examples include array key/value pairs,
124+
function arguments, closure `use` statements, `match()` statement branches, etc.
125+
126+
If that list is contained on a single line, then the last item MUST NOT have a trailing comma.
127+
128+
If the list is split across multiple lines, then the last item MUST have a trailing comma.
129+
130+
The following are examples of correct comma placement:
131+
132+
```php
133+
function beep(string $a, string $b, string $c)
134+
{
135+
// ...
136+
}
137+
138+
function beep(
139+
string $a,
140+
string $b,
141+
string $c,
142+
) {
143+
// ...
144+
}
145+
146+
$arr = ['a' => 'A', 'b' => 'B', 'c' => 'C'];
147+
148+
$arr = [
149+
'a' => 'A',
150+
'b' => 'B',
151+
'c' => 'C',
152+
];
153+
154+
$result = match ($a) {
155+
'foo' => 'Foo',
156+
'bar' => 'Bar',
157+
default => 'Baz',
158+
};
159+
```
160+
120161
## 3. Declare Statements, Namespace, and Import Statements
121162

122163
The header of a PHP file may consist of a number of different blocks. If present,

0 commit comments

Comments
 (0)