LoginSignup
0

More than 1 year has passed since last update.

PHP 三項演算子の省略形

Last updated at Posted at 2022-06-01

三項演算子の省略形

三項演算子は可読性が下がる場合もありますが、今回は紹介します。
下記の2つは同じ結果になります。

例文
$hoge ? $hoge : $hogehoge;

解説
「$hoge」が true の場合は「$hoge」を返し、 false の場合は「$hogehoge」を返します。
 

例文
$hoge ?: $hogehoge;

解説
「$hoge」が true の場合は「$hoge」を返し、 false の場合は「$hogehoge」を返します。
1つ目のコードと違う点は、 true の場合に返す値を省略している点です。
true の場合には「?」の左にある値が返されます。
※三項演算子の時点で可読性が下がる場合があり、省略することで更に可読性が下がる場合もあります。

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