25

How to check if array variable

$a = array('a'=>1, 'c'=>null);

is set and is null.

function check($array, $key)
{
    if (isset($array[$key])) {
        if (is_null($array[$key])) {
            echo $key . ' is null';
        }
        echo $key . ' is set';
    }
}

check($a, 'a');
check($a, 'b');
check($a, 'c');

Is it possible in PHP to have function which will check if $a['c'] is null and if $a['b'] exist without "PHP Notice: ..." errors?

Bartek Kosa
  • 802
  • 1
  • 12
  • 25

2 Answers2

53

Use array_key_exists() instead of isset(), because isset() will return false if the variable is null, whereas array_key_exists() just checks if the key exists in the array:

function check($array, $key)
{
    if(array_key_exists($key, $array)) {
        if (is_null($array[$key])) {
            echo $key . ' is null';
        } else {
            echo $key . ' is set';
        }
    }
}
nickb
  • 56,839
  • 11
  • 91
  • 130
  • @pulzarraider why did you replace `is_null()` with `===`? – Ivanka Todorova Jun 29 '16 at 08:38
  • Because it's faster. – pulzarraider Jun 29 '16 at 08:42
  • @pulzarraider - Any evidence to back up that claim? Because [this post](http://stackoverflow.com/questions/8228837/is-nullx-vs-x-null-in-php) and [this post](http://php.net/manual/en/function.is-null.php#84161) seems to heavily disagree with you, saving nanoseconds at best and making your edit a superfluous micro-optimization. – nickb Jun 29 '16 at 13:20
  • Yes, it's microoptimization, but why not make your function the fastest as it can be? Isset() is a bit slower because it's function call. – pulzarraider Jun 30 '16 at 10:52
-1

You may pass it by reference:

function check(&$array, $key)
{
    if (isset($array[$key])) {
        if (is_null($array[$key])) {
            echo $key . ' is null';
        }
        echo $key . ' is set';
    }
}

check($a, 'a');
check($a, 'b');
check($a, 'c');

SHould give no notice

But isset will return false on null values. You may try array_key_exists instead

RiaD
  • 42,649
  • 10
  • 67
  • 110
  • 1
    The original code also doesn't give a notice. Passing by reference is unnecessary. – Barmar Jul 20 '13 at 22:01
  • @nickb, you post answer while I was checking if things I remember were right:) – RiaD Jul 20 '13 at 22:03
  • @Barmar, yeah, my bad – RiaD Jul 20 '13 at 22:04
  • It is what I wanted. Because of isset returning false on null I will do it like this: if(!isset($array[$key]) && is_null($array[$key])) which equals true if $array[$key] is null. – Bartek Kosa Jul 20 '13 at 22:10
  • @BartekKosa your implementation is flawed. It will also return true and notice for keys that don't exists. – dev-null-dweller Jul 20 '13 at 22:15
  • My final version would be if(is_null(...){ catch all nulls}elseif(isset(...)){ catch all set variables}. But the most important trick is passing an array by reference to get rid of "Notice". – Bartek Kosa Jul 20 '13 at 22:18
  • Have you tested this? I believe isset() always returns false for null values even if they "exist" thus the is_null() will never be reached. The problem from the OP question remains. – thomasrutter Jan 09 '14 at 23:35