Note: empty array is converted to null by non-strict equal '==' comparison. Use is_null() or '===' if there is possible of getting empty array.
$a = array();
$a == null <== return true
$a === null < == return false
is_null($a) <== return false
The special NULL
value represents a variable with no value. NULL
is the
only possible value of type null.
A variable is considered to be null if:
it has been assigned the constant NULL
.
it has not been set to any value yet.
it has been unset().
There is only one value of type null, and that is the
case-insensitive constant NULL
.
<?php
$var = NULL;
?>
NULL
Casting a variable to null using (unset) $var
will not remove the variable or unset its value.
It will only return a NULL
value.
Note: empty array is converted to null by non-strict equal '==' comparison. Use is_null() or '===' if there is possible of getting empty array.
$a = array();
$a == null <== return true
$a === null < == return false
is_null($a) <== return false
Watch out. You can define a new constant with the name NULL with define("NULL","FOO");. But you must use the function constant("NULL"); to get it's value. NULL without the function call to the constant() function will still retrieve the special type NULL value.
Within a class there is no problem, as const NULL="Foo"; will be accessible as myClass::NULL.
Funny. It looks like, that there is one, and only one possible value for variable $a that will pass this test:
($a != NULL) && ((bool)$a == NULL)
It's "0" and it works because casting string "0" to boolean gives FALSE (and it's the only non empty string, that works this way). So remember that casting is not "transitive".
Be careful using NULL together with namespaces. If a NULL constant is redefined in a namespace other than global, you will get unexpected results when comparing to NULL inside the namespace. Instead always use \NULL, \FALSE, and \TRUE when comparing. Otherwise it may lead to application failures and potential security issues where certain checks could be effectively disabled.
A simple example to demonstrate the behavior:
<?php
namespace RedefinedConstants {
// redefining global namespace constants has no effect
define('NULL', 'I am not global NULL!');
define('TRUE', 'I am not global TRUE!');
define('FALSE', 'I am not global FALSE!');
// redefining local namespace constants will work
define('RedefinedConstants\NULL', 'I am not NULL!', \TRUE);
define('RedefinedConstants\FALSE', 'I am not FALSE!', \TRUE);
define('RedefinedConstants\TRUE', 'I am not TRUE!', \TRUE);
var_dump(
NULL, \NULL, null, \null, Null, \Null,
FALSE, \FALSE, false, \false, False, \False,
TRUE, \TRUE, true, \true, True, \True
);
}
?>
a quick note about the magic function __get() :
<?php
class Foo{
protected $bar;
public function __construct(){
$this->bar = NULL;
var_dump( $this->bar ); //prit 'NULL' but won't call the magic method __get()
unset( $this->bar );
var_dump( $this->bar ); //print 'GET bar' and 'NULL'
}
public function __get( $var ){ echo "GET " . $var; }
}
new Foo();
?>
Nulls are almost the same as unset variables and it is hard to tell the difference without creating errors from the interpreter:
<?php
$var = NULL;
?>
isset($var) is FALSE
empty($var) is TRUE
is_null($var) is TRUE
isset($novar) is FALSE
empty($novar) is TRUE
is_null($novar) gives an Undefined variable error
$var IS in the symbol table (from get_defined_vars())
$var CAN be used as an argument or an expression.
So, in most cases I found that we needed to use !isset($var) intead of is_null($var) and then set $var = NULL if the variable needs to be used later to guarantee that $var is a valid variable with a NULL value instead of being undefined.
Valiable values of (almost) all types that can be juggled to something "like" NULL will eval to NULL:
<?php
$test['_string'] = "";
$test['_int'] = 0;
$test['_float'] = 0.0;
$test['_null'] = null;
$test['_arr'] = array();
$test['_obj'] = new \stdClass();
foreach($test as $key => $value)
{
echo $key.": ";
if($value == null)
echo "is null\n";
else
echo "is not null\n";
}die();
?>
will give you
_string: is null
_int: is null
_float: is null
_null: is null
_arr: is null
_obj: is not null
// Difference between "unset($a);" and "$a = NULL;" :
<?php
// unset($a)
$a = 5;
$b = & $a;
unset($a);
print "b $b "; // b 5
// $a = NULL; (better I think)
$a = 5;
$b = & $a;
$a = NULL;
print "b $b "; // b
print(! isset($b)); // 1
?>