isset() VS array_key_exists() in PHP

  • Avinash
  • 10
  • Feb 07, 2012
  • Web Development

Here is the quick article which shows the difference between two PHP functions which are isset() and array_key_exists().

One basic difference is that isset() can be used with array and variables both while array_key_exists() can be used with the arrays only.

But the major difference lies in the values they return on certain condition. Main difference comes into picture while you are dealing with the NULL values.

So now lets check the major difference now.

Both this function return TRUE on success and FALSE on error. Here success means variable is available and error means variable is not available.

array_key_exists()

array_key_exists() will check for the existence of the key only. This function will return true even if that array element contain the NULL. Have a look at below code block for the same.

$arr = array( "one"=>"1", "two"=>"2", "three"=>null ); array_key_exists("one", $arr); // true array_key_exists("two", $arr); // true array_key_exists("three", $arr); // true
   $arr = array(
    "one"=>"1",
    "two"=>"2",
    "three"=>null
   );
   
   array_key_exists("one", $arr); // true
   array_key_exists("two", $arr); // true
   array_key_exists("three", $arr); // true

isset()

Unlike array_key_exists(), isset() will check the existence plus the value of the varible. This function will return true only if variable is exists and contain non null value. So this function will return false even if variable is exists but contain the NULL value.

Have a look at below code block for the same:

$arr = array( "one"=>"1", "two"=>"2", "three"=>null ); isset($arr["one"]); // true isset($arr["two"]); // true isset($arr["three"]); // false
   $arr = array(
    "one"=>"1",
    "two"=>"2",
    "three"=>null
   );
   
   isset($arr["one"]); // true
   isset($arr["two"]); // true
   isset($arr["three"]); // false

Conclusion

Here is the difference between these two functions, now be careful while using any of the function mentioned above.

Subscribe to our RSS feed for more PHP tips.

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by:

  • http://dewep.net Dewep.net

    Hi,

    Thank you for this information. :)

  • Peter Menis

    Isset-Example:
    Second “two” has to be “three” :D

  • http://irmantas.com Irmantas

    I think there should be ‘three’ => null in isset() code part.

  • http://www.phpgangsta.de PHPGangsta

    Hi Avinash,

    I think the third array element in the isset() example and the third isset() call has to be “three” not “two”? Otherwise it doesn’t make sense I think…

    Michael

  • Steve

    Did you put this on purpose?

    $arr = array( "one"=>"1", "two"=>"2", "two"=>null ); isset($arr["one"]); // true isset($arr["two"]); // true isset($arr["two"]); // false
       $arr = array(
        "one"=>"1",
        "two"=>"2",
        "two"=>null
       );
       
       isset($arr["one"]); // true
       isset($arr["two"]); // true
       isset($arr["two"]); // false

    I think you probably meant:

    $arr = array( "one"=>"1", "two"=>"2", "three"=>null ); isset($arr["one"]); // true isset($arr["two"]); // true isset($arr["three"]); // false
       $arr = array(
        "one"=>"1",
        "two"=>"2",
        "three"=>null
       );
       
       isset($arr["one"]); // true
       isset($arr["two"]); // true
       isset($arr["three"]); // false
  • ogondza

    The second example should probably use:

    $arr = array( "one"=>"1", "two"=>"2", "three"=>null );
    $arr = array(
        "one"=>"1",
        "two"=>"2",
        "three"=>null
    );

    However, there is one more difference between the two. array_key_exists requires second argument to be declared and to be an array. This code has serious issues but it silently returns true:

    $string = ""; isset ( $string["key"] ); // false isset ( $noSuchVar["key"] ); // false
    $string = "";
    isset ( $string["key"] ); // false
    isset ( $noSuchVar["key"] ); // false

    There is really very few reasons to use isset in well written code.

  • http://www.xpertdeveloper.com Avinash

    thanks to you all for notifying the mistake in code , corrected now :)

  • http://kaning.co.uk Kwabena Aning

    I don’t think you are evaluation these two on the right basis. The array_key_exists(“three”, $arr) will always return true because the KEY exists. isset($arr[‘three’]) however is checking the value stored in the “three” index of $arr which will return false because the VALUE is null.

    I think you are comparing apples to pears here mate.

    • http://www.xpertdeveloper.com Avinash

      My main concern for this article is for isset() function, because most of the developer thinks this function is used to check the existence of the variable but its not like that. It will check the existence plus value of that variable also. while array_key_exists() will not check for the value of the variable anyhow.

      • http://kaning.co.uk Kwabena Aning

        That’s fair enough. I didn’t get that from the post.