Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

issetとempty

Posted at

issetとempty

条件分岐とよく使う関数

isset

変数が定義されていて、null以外の値の時にtrueを返す。

$a = 0;
$b = 1;

if(isset($a)){
  echo 'true';
}else{
  echo 'false';
}

変数$aが定義されていて、なおかつ「0」なのでnullでもない。
つまりtrueを返す!

実行結果

true

empty

issetがfalse、または値がfalsyな場合にtrueを返す。
変数が空ならtrue。issetと逆。

$a = 0;
$b = 1;

if(empty($a)){
  echo 'true';
}else{
  echo 'false';
}

実行結果

true

変数$aは「0」が定義されているが、「0」はfalsyな値とされるので、
trueを返す!これはややこしい。

よく使われる形(否定の論理演算子)

$a = 0;
$b = 1;

if(!empty($a)){
  echo 'true';
}else{
  echo 'false';
}

実行結果

false

$aが空ではない時、true。
空の時、falseとなるので、
0はfalseyのためfalseが実行される。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?