In PHP there are numerous ways of checking if a variable, array, or array key is set, and if it has a value. It is important to check, to avoid the NOTICE: undefined index/variable
error message. Since there are so many ways to say false
and each function handles it differently, I am really looking to get a good overview of everything, so that I can make a good decision of what I should use when.
Here are the different options, please let me know if I forgot any. Which one of these do the same thing, which are the best to use in what situation?
// ARRAYS //
if(array_key_exists($key, $array))
if(isset($array[$key]))
if(!empty($array[$key]))
if(count($array[$key]))
if( $array[$key] === array() )
if( $array[$key] === '' )
if( $array[$key] === NULL )
if( $array[$key] === 0 )
if( $array[$key] === false )
if( $array[$key] == array() )
if( $array[$key] == '' )
if( $array[$key] == NULL )
if( $array[$key] == 0 )
if( $array[$key] == false )
// VARIABLES //
if(isset($variable))
if(!empty($variable))
if( $variable === '' )
if( $variable === NULL )
if( $variable === 0 )
if( $variable === false )
if( $variable == '' )
if( $variable == NULL )
if( $variable == 0 )
if( $variable == false )
What's the difference between these different ways of checking variables/arrays, and when should I use which to avoid NOTICE: undefined index/variable
?