0
0

More than 3 years have passed since last update.

emptyとissetの違い

Posted at

php言語をおけるemptyとissetの違いについて

emptyは空かどうかを判定し、空ならtrueを返す。

逆にissetは入っているかを判定し、入っていればtrueを返す。

だから、下記のような認識をしていた。

sample.php
empty === !isset

しかし、これは間違っています。

それは、0の判定の場合です。

emptyは「0」は「空」と判定します。

一方issetは「0」は「入っている」と判定します。

また、issetは空の配列も「入っている」と判定します。

ここの部分がemptyとissetの違いです。

ですから、その時の状況によってemptyとissetを使い分けるようにしましょう。

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