Skip to content

Instantly share code, notes, and snippets.

@syamgot
Last active July 27, 2017 01:37
Show Gist options
  • Save syamgot/f5cd0e8cbef77e927777942ad261d0d3 to your computer and use it in GitHub Desktop.
Save syamgot/f5cd0e8cbef77e927777942ad261d0d3 to your computer and use it in GitHub Desktop.
[PHP]issetとarray_key_existsの違い
<?php
$arr = ['NULL' => NULL,'ゼロ' => 0, '空文字' => ''];
//
echo "isset : \n";
foreach($arr as $key => $val) {
echo $key.'は'
. ( isset($arr[$key]) ? 'TRUE' : 'FALSE' )
. "\n";
}
echo "\n";
echo "array_key_exists : \n";
foreach($arr as $key => $val) {
echo $key.'は'
. ( array_key_exists($key, $arr) ? 'TRUE' : 'FALSE' )
. "\n";
}
/* ****************************************
isset :
NULLはFALSE
ゼロはTRUE
空文字はTRUE
array_key_exists :
NULLはTRUE
ゼロはTRUE
空文字はTRUE
issetは変数が存在してかつNULLではないかの判定なので
キーが存在するかしないかはarray_key_existsを使う
**************************************** */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment