0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【PHP】コードでよく見るスタイルの違う書き方、結局どっちが良いの?【コーディングスタイル】

0
Last updated at Posted at 2026-07-30

はじめに

プロジェクト内で書いている人によってスタイルの異なる文法がいくつかあったので、PHPの標準コーディング規約を基にどのスタイルが良いのか調べてみました。

良い書き方はPER Coding Style (以下PER)、PSR-12の標準をもとに判断しています。
標準で判断できないものは個人的な見解に基づいて判断しています。

本文

アロー関数

// ✅ Good
fn($num) => $num * $num;

// ❌ Bad
fn ($num) => $num * $num;

無名関数のfunction () => ...はfunctionの後にスペースが必要なので、アロー関数もfnの後にスペースが必要だと思っていました。
標準的なスタイルはスペースを入れないみたいです。

アロー関数はPHP7.4で追加された文法です。
そのため、PERで新たに標準的なスタイルが定義されています。

7.1 Short Closures
The fn keyword MUST NOT be succeeded by a space.

引用元:PER Coding Style

論理否定演算子 (!)

// ✅ Good
if (!$bool) {
// ...

// Not bad ?
if (! $bool) {
// ...

PERに単項演算子の記載はあるのですが、ここで明言されているのはインクリメント・デクリメントとキャスト演算子だけでした。

スペースが入っている!もよく見ますが、符号のように項に係っていることを強調したいので個人的にはスペースは入れない派です。

6.1. Unary operators
The increment/decrement operators MUST NOT have any space between the operator and operand:

$i++;
++$j;

Type casting operators MUST NOT have any space within the parentheses and MUST be separated from the variable they are operating on by exactly one space:

$intValue = (int) $input;

引用元:PER Coding Style

複合型の書き方

// ✅ Good
public function foo(string|int $bar, string|int $baz)
{
// ...

// ❌ Bad
public function foo(string | int $bar, string | int $baz)
{
// ...

こうして見ると標準の方が引数ごとにまとまって見えるので可視性が良さそうです。

複合型であるUNION型(|)はPHP8.0、交差型(&)はPHP8.1で追加されました。
そのためPSR-12には記載がなく、PERで新たに定義されています。

The union symbol | and intersection symbol & MUST NOT have a leading or trailing space.

引用元:PER Coding Style

末尾のカンマ

// ✅ Good
public function foo(
    string $a,
    string $b,
    string $c,
) {
// ...

// ❌ Bad
public function foo(
    string $a,
    string $b,
    string $c
) {
// ...

個人的に引数の順番を入れ替えるときにカンマを付けたり、消したりしなくても良いので結構好きなスタイルです。

PHP7.3で関数コールの引数リストの末尾、PHP8.0から関数定義の引数リストの末尾にもカンマが付けられるようになりました。
古いPHPのバージョンではエラーになってしまいますが、現在は配列や引数などが複数行になる場合は末尾にカンマを付けるのが標準となっています。

2.6 Trailing commas
If that list is contained on a single line, then the last item MUST NOT have a trailing comma.
If the list is split across multiple lines, then the last item MUST have a trailing comma.

引用元:PER Coding Style

&&や||を使った複数行にわたる条件式

// ✅ Good
if (
    $foo
    && $foobar
    && $bar
) {
//...

// ❌ Bad
if (
    $foo &&
    $foobar &&
    $bar
) {
//...

PSR-12では先頭・末尾のどちらかで揃えていれば大丈夫でしたが、PERでは先頭に揃えることと変更されています。

私は末尾に付けてしまうことが多かったのですが、末尾だと演算子の位置が揃わないので先頭のほうがきれいに見えて良さそうです。

Boolean operators between conditions MUST always be at the beginning.

引用元:PER Coding Style

Boolean operators between conditions MUST always be at the beginning or at the end of the line, not a mix of both.

引用元:PSR-12: Extended Coding Style

elseif と else if

// ✅ Good
//...
} elseif ($foo) {
//...

// ❌ Bad
//...
} else if ($foo) {
//...

elseifとelse ifのどちらで書いても動作は変わらないです。
ただ、PHPではif (...):のようにif文などのブロックの開始記号として:を使うことができ、この場合はelse ifだとエラーになるので注意が必要です。

PERではelseifのほうが推奨されています。

5.1 if, elseif, else
The keyword elseif SHOULD be used instead of else if so that all control keywords look like single words.

引用元:PER Coding Style

まとめ

コーディング規約もPHPの進化とともに発展しており、この記事を書くにあたって新しい標準について知ることができました。

他にも「この文法はどっちのスタイルが良いのだろう」みたいな書き方があれば、コメントしていただけると嬉しいです!

実際にはプロジェクトでコーディングスタイルが決められていることが多いので、標準のコーディングスタイルよりもそちらを優先するようにしましょう。

参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?