LoginSignup
5
5

More than 5 years have passed since last update.

php isset vs array_key_exists

Posted at

array_key_existsはキーがあるかをチェック。
issetは変数がセットされていること、そして NULL でないことを検査する。(array以外にも使用できる)

$a = array('key1' => 'hoge', 'key2' => null);

var_dump(isset($a['key1']));             // true
var_dump(array_key_exists('key1', $a));  // true

var_dump(isset($a['key2']));             // false
var_dump(array_key_exists('key2', $a));  // true

issetは変数がセットされていることを確認するので、変数が定義されていなくてもPHP Noticeが発生しない。

// $bが定義されていない
isset($b[4]);
array_key_exists('key2', $b); //PHP Notice, Warning発生
5
5
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
5
5