1
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?

More than 5 years have passed since last update.

Null 合体演算子(??)とエルビス演算子(?:)(三項演算子(A?B:C))の違いについて

Last updated at Posted at 2019-09-26

空白の場合

// null 合体演算子は空白は、true 扱い
>>> '' ?? false
=> ""

// エルビス演算子と三項演算子は、fales 扱い
>>> '' ?: false
=> false
>>> '' ? true : false
=> false

undefined の場合

// $hoge は未定義の変数
>>> $hoge
PHP Notice:  Undefined variable: hoge in Psy Shell code on line 1

// null 合体演算子は空白は、評価でき、fales 扱い
>>> $hoge ?? ''
=> ""

// エルビス演算子と三項演算子は、評価できない
>>> $hoge ?: ''
PHP Notice:  Undefined variable: hoge in Psy Shell code on line 1
>>> $hoge ? 'hoge'  : ''
PHP Notice:  Undefined variable: hoge in Psy Shell code on line 1
1
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
1
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?