How to reset a variable to NULL in PHP?
As nacmartin said, unset
will "undefine" a variable. You could also set the variable to null, however this is how the two approaches differ:
$x = 3; $y = 4;isset($x); // true;isset($y); // true;$x = null;unset($y);isset($x); // falseisset($y); // falseecho $x; // nullecho $y; // PHP Notice (y not defined)
Further explanation:
While a variable can be null or not null, a variable can also be said to be set or not set.
to set a variable to null, you simply
$var = null;
This will make $var
null, equivalent to false
, 0
and so on. You will still be able to get the variable from $GLOBALS['var']
since it is still defined / set. However, to remove a variable from the global and/or local namespace, you use the
unset($var);
This will make $var
not set at all. You won't be able to find in $GLOBALS
.