is_null

(PHP 4 >= 4.0.4, PHP 5)

is_null --  检测变量是否为 NULL

描述

bool is_null ( mixed var )

如果 varnull 则返回 TRUE,否则返回 FALSE

查看 NULL 类型获知变量什么时候被认为是 NULL,而什么时候不是。

参见 NULLis_bool()is_numeric()is_float()is_int()is_string()is_object()is_array()is_integer()is_real()


add a note add a note User Contributed Notes
jacob at gnu dot org
16-Sep-2005 02:45
The notes from the other users are confusing on what happens when a value from a mysql query is NULL (SQL NULL).  After doing a few tests, it looks like the way to test if the value is NULL or not is to use is_null($whatever)
also, it looks like this is functionally the same as (! isset($whatever) )
when you set $whatever  =NULL, isset($whatever) returns false now...
is ! isset($whatever) == is_null($whatever) for all versions of php? 
btw, the reason why people want to check for the NULL in the application is when they use a query that returns multiple rows of multiple columns where some columns are NULL is some rows, but not others and they don't want to have multiple SELECT queries for each possible outcome.
MARSIK
31-Jul-2005 10:54
I've tested different values in order to compare 'em with NULL with the help of different operators...

<?php
$arr
=array(0, 0.0, '0', '0.0', '',FALSE,'false',NULL, 'NULL');
for (
$i=0; $i<count($arr); $i++)
  
$arr[$i]=array(
    
$arr[$i],
     ((integer)(
$arr[$i]==NULL))
     .((integer)(
$arr[$i]===NULL))
     .((integer)
is_null($arr[$i]))
     );

var_dump($arr);
?>

it gave the following results:

0 : ==NULL
0.0 : ==NULL
'0' : nothing worked =)
'0.0' : nothing...
'' : ==NULL
FALSE : ==NULL
'false' : nothing
NULL : ==NULL, ===NULL, is_null()
'NULL' : nothing

enjoy =)
disappear at dissolution dot ath dot cx
09-May-2005 11:51
i wrote a small case study to compare is_null / == NULL / === NULL

Here's the code ::

<?php

   $Array
= array ( 0 , '' , FALSE , NULL ) ;
  
$ArrayCount = count ( $Array ) ;

  
$String .= '$Array = ' . "array ( 0 , '' , FALSE , NULL ) <br><br>" ;

   for (
$i = 0 ; $i < $ArrayCount ; $i++ )

   {

       if (
$Array [ $i ] == NULL )

       {

          
$String .= '$Array [ $i ] == NULL :: $Array [ ' . $i . ' ] <br>' ;

       }

       if (
$Array [ $i ] === NULL )

       {

          
$String .= '$Array [ $i ] === NULL :: $Array [ ' . $i . ' ] <br>' ;

       }

       if (
is_null ( $Array [ $i ] ) )

       {

          
$String .= 'is_null ( $Array [ $i ] ) :: $Array [ ' . $i . ' ] <br>' ;

       }

   }

   echo
$String ;

?>

Here's the results i got ::

$Array = array ( 0 , '' , FALSE , NULL )

$Array [ $i ] == NULL :: $Array [ 0 ]
$Array [ $i ] == NULL :: $Array [ 1 ]
$Array [ $i ] == NULL :: $Array [ 2 ]
$Array [ $i ] == NULL :: $Array [ 3 ]
$Array [ $i ] === NULL :: $Array [ 3 ]
is_null ( $Array [ $i ] ) :: $Array [ 3 ]
jbeninger at nospam tiberDot ca
12-Apr-2004 05:18
Regarding the function that returns a default value if the passed value is null: I like my functions names to bear resemblance to their function (Nz doesn't really pass that test). 

I've got a similar function but have called it "if_null" - after the SQL function that serves the same purpose.
michael at cannonbose dot com
31-Dec-2003 01:42
Regarding avoidance of NULLs in your MySQL queries, why not use  IS NULL and IS NOT NULL in your WHERE clauses.

SELECT *
FROM someDatabase
WHERE someAttribute IS NOT NULL

Cheers,

Michael
galardi at dii dot unisi dot it
20-Aug-2002 01:43
The following function is useful for set a variable to a default value the case it contains null:

function Nz($TestVar,$ValueIfNull)
{
   if (is_null($TestVar))
     return $ValueIfNull;
   else
     return $TestVar;
}

I have used it to ensure to always write something in the cells of a table; otherwise Netscape doesn't show the color of cell background. Ex:

<TD valign="top" BGCOLOR="#F0F0F0">
<?echo Nz($row2["Name"],"&nbsp;");?>
</TD>
dstone[at]NOSPAMviatraining.com
08-Jun-2002 09:19
The above example is a little wrong. An empty value is not equivalent to null in mysql. To get not null in mysql use "... where column is not null"

If you are doing a left join in mysql, no matched entry returns a null, where if you had a matched entry and were asking about a text field, '' might match, so != '' is different than is not null
thepissboy at hotmail dot com
07-May-2002 12:00
After pulling out most of my hair weeding through really unintelligible crap:

HERE'S AN EASY WAY TO ELIMINATE NULL VALUES FROM YOUR ARRAYS!!

$result = mysql_query("SELECT * FROM someDatabase WHERE page_title != ''",$db);
   if ($myrow = mysql_fetch_array($result)) {
  
   printf("%s\n", $myrow["page_title"]);
   }
  
   else {
  
   printf("Scratch head");
   }

It's all in the SQL Query line. The code says don't bring back anything that's empty!!!!

So hopefully, some of us will be able to get back to being in a hurry.
uioreanu at hotmail dot com
23-Mar-2001 10:36
Don't try to test
if ($intSomething==NULL) {
 ...
}
use is_null() instead.
The first statement misses 0 values.

Regards,
Calin

[Ed. note: this is because == tests for equivalence of value, but not type. NULL evaluates to
false, as does 0, so NULL == 0 is true--even though 0 is type int and NULL is type null.
You should use either is_null() as noted or ===, which returns true only if its operands are
equal and of the same type.]