What is the difference in checking an array with isset and array_key_exists?

4

In PHP we often have more than one way of doing the same operation. A basic example is in checking for the existence of a given index in array : we can use array_keys_exists .

Example:

$arr = ['a' => 1, 'b' => 2];

var_dump(isset($arr['a'])); // bool(true)

var_dump(array_key_exists($arr, 'a')); //bool(true)

Since the results are the same, I would like to know:

  • Is there any difference between isset and array_keys_ exists when checking for index existence?

  • Is there a difference in performance?

  • When should I use one or the other?

asked by anonymous 10.03.2016 / 17:30

3 answers

5

I gave a brief explanation on this answer link I will try to better define the difference here as per the questions asked

Reply to:

  
  • Is there any difference between isset and array_keys_ exists when checking for index existence?
  •   

    and

         
  • When should I use one or the other?
  •   
    • array_key_exists

      The% w_of% just checking whether the key exists in an array by returning a value of array_key_exists , should be used only when you need to know if a key specifies exists in an array, even if it has the value bool it will still return null if the key exists, eg:

      $foo = array( 'baz' => 0 );
      
      var_dump(array_key_exists('baz', $foo));
      
    • isset

      true is not a function, but the important thing is to understand how it works, the first difference is that isset supports multiple checks like this:

      isset($foo['baz'], $foo['bar'], $_POST['foo']);
      

      Of course the result varies as you add elements.

      Another difference is that it does not only check for isset , but for normal variables, for example if you do this in an undefined variable it does not issue Warning:

      <?php isset($foo); ?>
      

      But this issues the mensem Undefined variable :

      <?php echo $foo; ?>
      

      This is because when trying to use arrays even if it does not exist, it goes "technically" and the value is $variável pro PHP.

      Another feature is that pro NULL returns isset if the variable or item of an array is false , even if it exists:

      $foo = NULL;
      
      var_dump($foo);
      
      $test = array( 'foo' => NULL );
      
      var_dump($test['foo']);
      
      

    Is there a difference in performance?

    This varies a lot, some say that null is not a function but rather a "constructor" is likely to be faster. In most cases this may be right, even if you consider older versions of PHP, but depending on the type of verification the difference is likely to be so insignificant that it's not even worth comparing (it's just a "micro-optimization" at most) , but what I say is that for most checking cases, isset will be better, for example as I mentioned the possibility of checking multiple items or variables.

      

    Note: To remove an item from an array use isset , it is possible to remove multiple items or variables unset

    Using empty

    Another function (or constructor) that may be interesting to use is unset($foo1, $foo2, $foo3); , it checks the value type, it will return empty for when the value of the item or variable is:

    • false (an empty string)
    • "" (when an integer equal to zero)
    • 0 (zero as string)
    • "0"
    • NULL
    • FALSE (an empty array)
    • array() (When a variable is declared in a class but has no value since it is NULL)
    10.03.2016 / 19:06
    6

    As our friend Papa Charlie said, the return varies according to the values in the array. This code will show you a different return of the two methods mentioned.

    <?php
    
    $arr1 = ['a' => 1, 'b' => 2];
    $arr2 = ['a' => null, 'b'=>2];
    
    var_dump(isset($arr1['a'])); // true
    var_dump(array_key_exists('a', $arr1)); // true
    
    var_dump(isset($arr2['a'])); // false
    var_dump(array_key_exists('a', $arr2)); // true
    
    ?>
    

    Isset helps you to check for a null key.

    Answering your questions ...

    Is there any difference between isset and array_keys_ exists when checking for index existence? A: For this check, not If the array has key and value completed, there is no difference. If the value is null, there is a difference.

  • Is there a difference in performance? A: Yes. isset () is faster, though not exactly the same as the other function.

  • >
  • When should I use one or the other? A: array_key_exists () checks whether the key (in this case "a") exists. But it does not check whether the value is null or not. The isset (), does this check and returns false (as in the code example posted above).

  • 10.03.2016 / 18:19
    4

    array_key_exists() will return it if the key exists or not in the array, regardless of the value it has. isset() will return you bool(true) only if the key exists in the array E its value is different from null .

    Another difference is that if you pass an undefined variable to isset() it only returns bool(false) , array_key_exists() starts a PHP WARNING on the screen and returns NULL :

    var_dump(isset($array['a'])); // bool(false)
    var_dump(array_key_exists('a', $array)); // lança um "PHP Notice:  Undefined variable" e imprime "NULL"
    
    $array = ['a' => null, 'b' => 2];
    
    var_dump(isset($array['a'])); // bool(false)
    var_dump(array_key_exists('a', $array)); // bool(true)
    
    var_dump(isset($array['b'])); // bool(true)
    var_dump(array_key_exists('b', $array)); // bool(true)
    

    So, use array_key_exists() when you want to know whether or not the key exists in the array (regardless of the value) and you are sure that the array exists , and isset() when you you want to make sure that the key exists and that it is different from null .

    See the code working at Ideone .

        
    10.03.2016 / 18:17