is_null

(PHP 4 >= 4.0.4, PHP 5)

is_null --  Finds whether a variable is NULL

Описание

bool is_null ( mixed var )

Finds whether the given variable is NULL.

Список параметров

var

The variable being evaluated.

Возвращаемые значения

Returns TRUE if var is null, FALSE otherwise.



is_null
jacob at gnu dot org
15-Sep-2005 11: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 07: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
08-May-2005 08: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
11-Apr-2004 02: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
30-Dec-2003 09: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
19-Aug-2002 10: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
07-Jun-2002 06: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
06-May-2002 09: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 06: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.]

<is_longis_numeric>
 Last updated: Tue, 15 Nov 2005