LoginSignup
0
0

More than 3 years have passed since last update.

PHPの2項演算子

Posted at

つぶやき

今更感あるけど、ついついif文や3項演算子を使ってしまうことも多く..
呼び方は2項演算子でいいのかな?

PHP7からの機能なので、PHP7未満では使えないです(今どきそうそうないでしょうけど)。

ifの場合

if ($value) {
  $result = $value;
} else {
  $result = 0;
}

3項演算子の場合

$result = $value ? $value : 0

2項演算子の場合

$result = $value ?? 0;
0
0
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
0