0

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?

oliver_siegel
  • 1,666
  • 3
  • 22
  • 37
  • I know [this](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) may sound like a similar question. But what's asked there is: `What does the NOTICE mean? What can I do to fix it?` What I am asking is: `What is the difference between these many different ways of fixing the NOTICE?` – oliver_siegel Jul 17 '14 at 19:12
  • 1
    Only the first three are actually "fixing the notice", everything else are standard comparisons, the result of which you can read in the manual. Perhaps you want this: [The Definitive Guide To PHP's isset And empty](http://kunststube.net/isset/) – deceze Jul 17 '14 at 19:14
  • 1
    Too broad, by a long shot. This question is all over the map, most of these things have nothing to do with each other. You should read up a few things: How do `===` and `==` differ, how to test whether an array index exists, how to test whether a variable has been defined, look up a PHP truth table, and then read about `is_set` and `is_empty` and `array_key_exists`, all of which are well documented. – user229044 Jul 17 '14 at 19:21
  • Say we have `$var = '';` won't `empty($var)` and `$var == 0`, `$var ==''`, `$var ==false`, and even `$var == NULL` return the same thing as `$var === ''`? – oliver_siegel Jul 17 '14 at 19:22
  • It doesn't matter whether the *evaluate* to the same thing (they don't "return" anything, and they definitely don't always evaluate to the same thing as `$var === ''`, again, look up `==` vs `===`). They're a bunch of unrelated statements. They don't serve the same purpose, and they don't test for the same thing. – user229044 Jul 17 '14 at 19:23
  • 1
    Really, 90% of this question are answered here: http://php.net/manual/en/types.comparisons.php. The other 10% are answered in the manual pages for `isset`, `empty` and `array_key_exists`, and more verbosely and with background story in the aforelinked article. – deceze Jul 17 '14 at 19:28
  • @meagar, thanks for clarifying _evaluate_ vs. _return_ – oliver_siegel Jul 17 '14 at 19:35
  • Big thanks to @deceze for two **super** helpful links!! Another I just found is: http://stackoverflow.com/questions/80646/how-do-the-php-equality-double-equals-and-identity-triple-equals-comp – oliver_siegel Jul 17 '14 at 19:36
  • Great, so now that I read the articles I know the answer to my own question. And I just see that you actually wrote the one article @deceze, thanks for that, explains a lot of things. – oliver_siegel Jul 17 '14 at 20:06

0 Answers0