PHP: is_null - Manual

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

search for in the

is_numeric is_long

Last updated: Fri, 26 Jan 2007

view this page in

# 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.

## Смотрите также

The `NULL` type
is_bool()
is_numeric()
is_float()
is_int()
is_string()
is_object()
is_array()

add a note User Contributed Notes
is_null
passion4code at gmail dot com
10-Nov-2006 10:25
`function value_is_null($t){
if (is_array($t)) {
return (size_of($t) > 0)
} else {
return (($t != '') && (strtolower($t) != 'null') && (strlen(trim($t)) > 0))
}
}
This is fairly generic and will account for a variety of parameters. If you really want to be sure, add a && !is_null($t) inside the last return statement.`
powderz at gmail dot com
11-Aug-2006 09:41
`Actually, since a wrapper is going to be written, you can check for your own version null values...if you want to be creative for some reason.
<?php
function isnull($data)
{
/** only if you need this
if (is_string($data)) {
$data = strtolower($data);
}
*/
switch ($data) {
// Add whatever your definition of null is
// This is just an example
//-----------------------------
case 'unknown': // continue
case 'undefined': // continue
//-----------------------------
case 'null': // continue
case 'NULL': // continue
case NULL:
return true;
}
// return false by default
return false;
}
?>`
powderz at gmail dot com
11-Aug-2006 09:18
`Hello all. The function is really simple, but it seems like others are making it confusing than it should be.
is_null() will return true if:
the variable has not been set, or the variable has the following values: NULL or null
<?php
$var = NULL;
$var = null;
$var;
// or if not declared at all
?>
without using is_null(), the proper way to check for null values is using === operator.
<?php
if ($var === NULL) {} // true
if ($var === null) {} // true
if (is_null($var)) {} // true
/*
* Any other values for $var will return false
*/
?>
With that in mind, you can save an extra function call by avoiding is_null() check and just use
<?php
if ($var === NULL) {}
// or
if ($var === null) {}
?>
When dealing with database nulls (I can't remember whether it's "null" or "NULL"), you're just going to have to write a wrapper function. Here's my untested function:
<?php
function isnull($data)
{
/*
* I can't remember the string null value the database returns, so I'm making two assumptions
*/
if ($data === NULL || $data === 'NULL' || $data === 'null') {
return true;
}
return false;
}
?>`
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.]`
add a note
is_numeric is_long

Last updated: Fri, 26 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 10:36:09 2007 PST