downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

Callbacks> <Resources
[edit] Last updated: Fri, 26 Apr 2013

view this page in

NULL

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().

Syntax

There is only one value of type null, and that is the case-insensitive constant NULL.

<?php
$var 
NULL;       
?>

See also the functions is_null() and unset().

Casting to 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.



Callbacks> <Resources
[edit] Last updated: Fri, 26 Apr 2013
 
add a note add a note User Contributed Notes NULL - [9 notes]
up
16
quickpick
2 years ago
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
up
3
foxdie_cs at hotmail dot com
9 months ago
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();
?>
up
1
nl-x at bita dot nl
5 years ago
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.
up
1
rizwan_nawaz786 at hotmail dot com
8 years ago
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.
up
0
cdcchen at hotmail dot com
6 years ago
empty() is_null() !isset()

$var = "";

empty($var) is true.
is_null($var) is false.
!isset($var) is false.
up
0
poutri_j at epitech dot net
7 years ago
if you declare something like this :

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

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

}
?>

be carefull, that's strange but an empty array is considered as a null variable
up
-2
dward at maidencreek dot com
11 years ago
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.
up
-3
ryan at trezshard dot com
1 year ago
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.
up
-5
Anonymous
7 years ago
// 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
?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites