What's the Difference Between Isset() and Array_Key_Exists()

Isset vs array_key_exists

See: http://us3.php.net/array_key_exists

isset() does not return TRUE for array keys that correspond to a NULL value, while array_key_exists() does.

Replacing array_key_exists() with isset() in PHP

In one project, I have the value of $var submitted by users.

How does that work? Users shouldn't be able to set variables. They should be able to, e.g., submit form values which end up in $_POST. I repeat, they should not be able to directly create variables in your scope.

If "users" here means some sort of plugin system where people write and include PHP code… then you may want to think about defining a more stable interface than setting variables.

The value can be anything, even NULL…

Not if it's a value submitted through HTTP. HTTP has no concept of null. It's either an empty string or doesn't exist at all.

using !empty($var) alone is dangerous since if $var isn't pre-defined, PHP will throw an error

That is wrong. empty specifically exists to test a variable against false without throwing an error. empty($var) is the same as !$var without triggering an error if the variable doesn't exist.

So I have isset($var) && !empty($var) for checking whether $var holds a non-empty value.

See Why check both isset() and !empty(). (Spoiler: it's redundant.)

echo isset($arr['foo']);
echo array_key_exists('foo', $arr) && !is_null($arr['foo']);

These both do exactly the same thing. isset returns true if the value exists and its value is not null. The second line returns true if the array key exists and its value is not null. Same thing.

I had bad experience...

You'd need to be more detailed about that, since there should be no caveat to isset as you describe it.

See The Definitive Guide To PHP's isset And empty and Difference between isset and array_key_exists.

What's quicker and better to determine if an array key exists in PHP?

isset() is faster, but it's not the same as array_key_exists().

array_key_exists() purely checks if the key exists, even if the value is NULL.

Whereas
isset() will return false if the key exist and value is NULL.

What is the difference between in_array() and array_key_exists()?

in_array() versus array_key_exists()

Difference:

  • in_array() checks if a value exists in an array (checks the values, not the keys) and returns true, or false otherwise.

while:

  • array_key_exists() checks if the given key or index exists in the array (checks in keys, not in values) and returns true, or false otherwise.

Visits the manual (links above) for examples and more information.

An example link: https://eval.in/602279

array_key_exists($key, $array) vs !empty($array[$key])

The other responses focus on the differences between the two functions. This is true, but if the source array does not contain null or 0 or "", ... (empty values) values you can benchmark the speed of the two functions:

<?php

function makeRandomArray( $length ) {
$array = array();
for ($i = 0; $i < $length; $i++) {
$array[$i] = rand(1, $length);
}

return $array;
}

function benchmark( $count, $function ) {
$start = microtime(true);
for ($i = 0; $i < $count; $i++) {
$function();
}
return microtime(true) - $start;
}

$runs = 100000;
$smallLength = 10;
$small = makeRandomArray($smallLength);

var_dump(benchmark($runs, function() {
global $small, $smallLength;
array_key_exists(rand(0, $smallLength), $small);
}));
var_dump(benchmark($runs, function() {
global $small, $smallLength;
!empty($small[rand(0, $smallLength)]);
}));

Which gave me the following results:

For a small array:

  • array_key_exists: float(0.18357992172241)
  • empty: float(0.072798013687134)
  • isset: float(0.070242881774902)

For a relative big array:

  • array_key_exists: float(0.57489585876465)
  • empty: float(0.0068421363830566)
  • isset: float(0.0069410800933838)

So if it's possible it's faster to use empty or isset.

How To Regex Search and Replace array_key_exists with isset?

The best I could do was to run a second search and replace to cover the instances that used variables for both arguments:

array_key_exists($my_key,$my_array)

search and replace 2:

  • search for:

    array_key_exists\s*\(\s*(\$[^,]*)\s*,([^)]*)\)

  • replace with:

    isset($2[$1])



Related Topics



Leave a reply



Submit