类型
在线手册:中文  英文

NULL

特殊的 NULL 值表示一个变量没有值。NULL 类型唯一可能的值就是 NULL

在下列情况下一个变量被认为是 NULL

语法

NULL 类型只有一个值,就是不区分大小写的常量 NULL

<?php
$var 
NULL;       
?>

参见 is_null()unset()

转换到 NULL

使用 (unset) $var 将一个变量转换为 null不会删除该变量或 unset 其值。仅是返回 NULL 值而已。


类型
在线手册:中文  英文

用户评论:

Toycat (2013-05-29 09:11:09)

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, \NULLnull, \nullNull, \Null,
        
FALSE, \FALSEfalse, \falseFalse, \False,
        
TRUE, \TRUEtrue, \trueTrue, \True
    
);

}
?>

foxdie_cs at hotmail dot com (2012-08-01 06:00:39)

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

ryan at trezshard dot com (2011-06-01 08:31:01)

This simple shorthand seems to work for setting new variables to NULL:

<?php
$Var
;
?>

The above code will set $Var to NULL

UPDATE: After further testing it appears the code only works in the global scope and does not work inside functions.

<?php
function Example(){
  
$Var;
  
var_dump($Var);
}
?>

Would not work as expected.

quickpick (2011-04-22 15:36:37)

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

nl-x at bita dot nl (2007-07-09 10:33:46)

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.

cdcchen at hotmail dot com (2006-05-25 20:17:45)

empty() is_null() !isset()
$var = "";
empty($var) is true.
is_null($var) is false.
!isset($var) is false.

(2006-01-06 01:51:13)

// 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 
?>

poutri_j at epitech dot net (2005-07-26 04:56:18)

if you declare something like this :

<?php
class toto
{
    public 
$a = array();

    public function 
load()
    {
        if (
$this->== null// ==> the result is true
            
$a other_func();
    }

}
?>

be carefull, that's strange but an empty array is considered as a null variable

rizwan_nawaz786 at hotmail dot com (2004-10-18 21:22:30)

Hi
Rizwan Here

Null is the Constant in PHP. it is use to assign a empty value to the variable like
$a=NULL;
At this time $a has is NULL or $a has no value;
When we declaire a veriable in other languages than that veriable has some value depending on the value of memory location at which it is pointed but in php when we declaire a veriable than php assign a NULL to a veriable.

dward at maidencreek dot com (2001-11-12 15:52:06)

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.

易百教程