Специальное значение NULL говорит о том, что эта переменная не имеет значения. NULL - это единственно возможное значение типа NULL.

Замечание: Пустой тип был введен в PHP 4

Переменная считается NULL если

  • ей была присвоена константа NULL.

  • ей еще не было присвоено какое-либо значение.

  • она была удалена с помощью unset().


Существует только одно значение типа NULL - регистро-независимое ключевое слово NULL.


Смотрите также is_null() и unset().


Nulls are almost the same as unset variables and it is hard to tell the difference without creating errors from the interpreter:


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.
2001-11-12 17:52:06
// Difference between "unset($a);" and "$a = NULL;" :
// unset($a)
$a 5;
$b = & $a;
"b $b "// b 5 

// $a = NULL; (better I think)
$a 5;
$b = & $a;
$a NULL;
"b $b "// b 
print(! isset($b)); // 1 
2006-01-06 03:51:13
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.
2007-07-09 13:33:46
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
2011-04-22 18:36:37
a quick note about the magic function __get() :

class Foo{
    public function 
$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; }

2012-08-01 09:00:39
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:

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);


2013-05-29 12:11:09
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".
2014-03-26 02:10:50
Valiable values of (almost) all types that can be juggled to something "like" NULL will eval to NULL:

['_string'] = "";
$test['_int'] = 0;
$test['_float'] = 0.0;
$test['_null'] = null;
$test['_arr'] = array();
$test['_obj'] = new stdClass();

$test as $key => $value)
$key.": ";
$value == null)
"is null\n";
"is not null\n";

will give you

_string: is null
_int: is null
_float: is null
_null: is null
_arr: is null
_obj: is not null
2015-12-02 17:39:38
NULL is supposed to indicate the absence of a value, rather than being thought of as a value itself. It's the empty slot, it's the missing information, it's the unanswered question. It's not a jumped-up zero or empty set.

This is why a variable containing a NULL is considered to be unset: it doesn't have a value. Setting a variable to NULL is telling it to forget its value without providing a replacement value to remember instead. The variable remains so that you can give it a proper value to remember later; this is especially important when the variable is an array element or object property.

It's a bit of semantic awkwardness to speak of a "null value", but if a variable can exist without having a value, the language and implementation have to have something to represent that situation. Because someone will ask. If only to see if the slot has been filled.
2017-12-07 13:07:56
Note: Non Strict Comparison '==' returns bool(true) for 

null == 0 <-- returns true

Use Strict Comparison Instead

null === 0 <-- returns false
2018-01-11 09:38:06

    Поддержать сайт на родительском проекте КГБ