PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<ResourcePseudo-types used in this documentation>
view the version of this page
Last updated: Sun, 02 May 2004

NULL

The special NULL value represents that a variable has no value. NULL is the only possible value of type NULL.

Nota: The null type was introduced in PHP 4

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 keyword NULL.

<?php
$var
= NULL;     
?>

See also is_null() and unset().



add a note add a note User Contributed Notes
NULL
pozmu at wp dot pl
21-Apr-2004 02:21
responding to joemamacow at hotmail dot com comment:
if you are using isset() to check is the variable set, the more logically and clear way to "delete" the variable is to use unset() (http://www.php.net/unset ) function:

<?php

unset($variable);

?>

Of course setiing variable value to NULL is also OK.
alex at netflex dot nl
13-Jun-2002 10:13
Hi,

Function for looking if it is a NULL

<?php

  $var
= NULL;

  if (
isnull("var")) {
   echo
"var===NULL\n";
  } else {
   echo
"var!==NULL\n";
  }

  if (
isnull("test")) { // give FALSE, test is not set
  
echo "test===NULL\n";
  } else {
   echo
"test!==NULL\n";
  }

 
$array['var'] = NULL;

  if (
isnull("var", $array)) {
   echo
"array['var']===NULL\n";
  } else {
   echo
"array['var']!==NULL\n";
  }

  function
isnull($var, $base = FALSE) {
   if (
$base===FALSE) {
    
$base = &$GLOBALS;
   } elseif (!
is_array($base)) {
     return
FALSE;
   }
   if ((
array_key_exists($var, $base))&&($base[$var]===NULL)) {
     return
TRUE;
   } else {
     return
FALSE;
   }
  }

?>
joemamacow at hotmail dot com
02-Mar-2002 08:12
I have particularly found that the NULL value is very helpful when using form and script on the same page.

For example:

if(isset($password) AND $password = $passwd):
{
echo "You're logged in!";
}
elseif(isset($password) AND $password != $passwd):
{
$password = NULL;
echo "Sorry, wrong password, <a href=$PHP_SELF>log in again</a>.";
}
else:
{
echo "Log in!
<input type=text name=password>
<input type=submit>";
}
endif;

So, when the user geta the wrong password message, the password is set to NULL, making it unset, therefore when the page is refreshed, it will go back to the password from.
avbentem at hetnet.nl
22-Feb-2002 07:25
To extend a bit on tbdavis's comment:

:: NULL == NULL is true
:: NULL == FALSE is true
:: NULL == TRUE is false

However: note the implicit type conversions that PHP performs! When using 'identical' instead of 'equal' then both NULL === FALSE and NULL === TRUE yield FALSE. An overview is easily created using something like

   function evalExpr( $desc )
   {
     echo str_pad($desc , 15) . "--> ";
     var_dump( eval( "return(" . $desc . ");" ));
   }

Note that even TRUE AND TRUE does not evaluate to a boolean value, and that OR and XOR behave different as well!

   PHP Version: 4.0.6

   false          --> bool(false)
   true          --> bool(true)
   null          --> NULL
   !null          --> bool(true)

   true and true  --> int(1)
   true and false --> int(0)
   true or true  --> int(1)
   true or false  --> int(1)
   true xor true  --> bool(false)
   true xor false --> bool(true)
 
   true == null  --> bool(false)
   true === null  --> bool(false)
   true != null  --> bool(true)
   true !== null  --> bool(true)

   false == null  --> bool(true)
   false === null --> bool(false)
   false != null  --> bool(false)
   false !== null --> bool(true)

   null == null  --> bool(true)
   null != null  --> bool(false)
   null === null  --> bool(true)
   null !== null  --> bool(false)

   null or null  --> int(0)
   null xor null  --> bool(false)
   null and null  --> int(0)
 
   true or null  --> int(1)
   true xor null  --> bool(true)
   true and null  --> int(0)

   false or null  --> int(0)
   false xor null --> bool(false)
   false and null --> int(0)

   true < null    --> bool(false)
   true > null    --> bool(true)
   false < null  --> bool(false)
   false > null  --> bool(false)

   1 + null      --> int(1)
   "text" . null  --> string(4) "text"

Finally, for those who do not know SQL: in SQL the NULL value is evaluated a bit like "I do not know; it could be anything, like 0, 1, a, b, true, false or even nothing at all". This implies that in SQL NULL == NULL could be interpreted as "could be anything" == "could be something else", which does not yield true! Instead, it yields NULL, which boils down to FALSE in boolean context...

Likewise, in SQL:

   NULL AND TRUE yields NULL
   NULL OR TRUE yields TRUE
   NULL AND FALSE yields FALSE
   NULL OR FALSE yields NULL
   NULL == TRUE yields FALSE
   NULL == FALSE yields FALSE

a.
sc at dbtech dot de
16-Feb-2002 09:59
NULL as the best way to detect additional parameters of unknown type:

function FooBar($Param = NULL) {
  if (is_null($Param)) {
  [...]
junk dot phpnet at gasolinemm dot com
18-Nov-2001 03:19
$a = "";
$b = NULL;

$a == $b;
/* returns true: $a has been converted to $b for the equality comparison */

is_null($a); //returns false
is_null($b); //returns true

$a === $b;
/* returns false: $a is not _identical_ to $b */
dward at maidencreek dot com
12-Nov-2001 09:52
Nulls are almost the same as unset variables and it is hard to tell the difference without creating errors from the interpreter:

$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.
tbdavis at greyshirt dot net
11-Oct-2001 10:36
Unlike the relational model, NULL in PHP has the following properties:
NULL == NULL is true,
NULL == FALSE is true.
And in line with the relational model, NULL == TRUE fails.

<ResourcePseudo-types used in this documentation>
 Last updated: Sun, 02 May 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2004 The PHP Group
All rights reserved.
This mirror generously provided by: Italia OnLine S.p.a.
Last updated: Fri May 21 04:11:23 2004 CEST