isset() VS array_key_exists() in PHP

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: