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?
You may pass it by reference:
SHould give no notice
But
isset
will returnfalse
on null values. You may tryarray_key_exists
insteadUse
array_key_exists()
instead ofisset()
, becauseisset()
will returnfalse
if the variable isnull
, whereasarray_key_exists()
just checks if the key exists in the array: