Web Development

isset() VS array_key_exists() in PHP

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

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 [code]NULL[/code] values.

So now lets check the major difference now.

Both this function return [code]TRUE[/code] on success and [code]FALSE[/code] 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 [code]NULL[/code]. Have a look at below code block for the same.

[cc lang=”php”]
$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
[/cc]

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:

[cc lang=”php”]
$arr = array(
“one”=>”1”,
“two”=>”2”,
“three”=>null
);

isset($arr[“one”]); // true
isset($arr[“two”]); // true
isset($arr[“three”]); // false
[/cc]

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.

Shares:
  • Maigret Aurélien
    February 7, 2012 at 11:50 pm

    Hi,

    Thank you for this information. :)

    Reply
  • Peter Menis
    Peter Menis
    February 8, 2012 at 1:34 pm

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

    Reply
  • Irmantas
    February 8, 2012 at 1:34 pm

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

    Reply
  • PHPGangsta
    February 8, 2012 at 4:44 pm

    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

    Reply
  • Steve
    Steve
    February 8, 2012 at 6:29 pm

    Did you put this on purpose?
    [cc lang=”php”]
    $arr = array(
    “one”=>”1”,
    “two”=>”2”,
    “two”=>null
    );

    isset($arr[“one”]); // true
    isset($arr[“two”]); // true
    isset($arr[“two”]); // false
    [/cc]

    I think you probably meant:
    [cc lang=”php”]
    $arr = array(
    “one”=>”1”,
    “two”=>”2”,
    “three”=>null
    );

    isset($arr[“one”]); // true
    isset($arr[“two”]); // true
    isset($arr[“three”]); // false
    [/cc]

    Reply
  • ogondza
    ogondza
    February 8, 2012 at 8:46 pm

    The second example should probably use:

    [cc lang=”php”]
    $arr = array(
    “one”=>”1”,
    “two”=>”2”,
    “three”=>null
    );
    [/cc]

    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:
    [cc lang=”php”]
    $string = “”;
    isset ( $string[“key”] ); // false
    isset ( $noSuchVar[“key”] ); // false
    [/cc]
    There is really very few reasons to use isset in well written code.

    Reply
  • Avinash
    February 8, 2012 at 9:30 pm

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

    Reply
  • Kwabena Aning
    February 8, 2012 at 9:53 pm

    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.

    Reply
    • Avinash
      February 8, 2012 at 10:58 pm

      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.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *