0
0

??null;って何よ。三項演算子も一緒に 備忘録

Last updated at Posted at 2024-08-20

chatgptさんにいつもの如く質問してたら、回答の一部にこんなのが…

$edit_id = $_POST["edit_id"] ?? null;

何言ってんだかよくわからなかったので再度chatgptさんに聞き直したら、
「PHP 7.0 で導入された null 合体演算子(null coalescing operator)」
と言われました。簡単に言っちゃば、三項演算子をもっと簡略化したやつ(?)みたい。

三項演算子

構文: 条件式 ? trueで出力される値 : falseで出力される値;

例:

echo isset($edit_comment) ? $edit_comment : ""; 

NULL合体演算子

構文: 値1 ?? 値2;
動作: 左側の値が null または未定義であれば、右側の値を返します。左側の値が null でなければ、その値を返す。
例:

$edit_id = $_POST["edit_id"] ?? null;

That's all.

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