How to check if variable has been Set To Null in PHP?

  • Thread starter Thread starter Fuad
  • Start date Start date
F

Fuad

Guest
I need to know if there is a way to find out if a variable has been set to a value of NULL.

Example:

$temp = null;

if(has_been_set_to_null($temp)){
echo 'yes';
}

If you use the function "is_null" ... that is not sufficient .. because it will return true for values that have not been defined as well. And cant use "isset" + "is_null" .. because "isset" checks if the variable is not null ;-)

So basically I need to find out if a variable has been set , for real, and the value is "Null"

Thanks in advance.
I am looking to detect that a variable has been initialized to NULL .. and not just an empty undefined variable.

Strong equal will allow an undefined variable to be equal to Null so it wont work.

Thanks you for the help in advance.
Hi,

I found the answer:

if(array_key_exists('a',get_defined_vars()) && is_null($a)){
//will only execute if variable has been defined and is set to Null
}

Where "a" is the variable name.

Thanks all for your help.
 
Back
Top