PHP Doku:: Prüft ob eine Variable NULL enthält - function.is-null.html

Verlauf / Chronik / History: (2) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzVariablen- und typbezogene ErweiterungenVariablenbehandlungFunktionen zur Behandlung von Variablenis_null

Ein Service von Reinhard Neidl - Webprogrammierung.

Funktionen zur Behandlung von Variablen

<<is_long

is_numeric>>

is_null

(PHP 4 >= 4.0.4, PHP 5)

is_null Prüft ob eine Variable NULL enthält

Beschreibung

bool is_null ( mixed $var )

Prüft ob die gegebene Variable NULL enthält

Parameter-Liste

var

Die zu prüfende Variable.

Rückgabewerte

Liefert TRUE wenn var null enthält, sonst FALSE.

Beispiele

Beispiel #1 is_null()-Beispiel

<?php

error_reporting
(E_ALL);

$foo NULL;
var_dump(is_null($inexistent), is_null($foo));

?>
Notice: Undefined variable: inexistent in ...
bool(true)
bool(true)

Siehe auch

  • Der NULL-Typ
  • isset() - Prüft, ob eine Variable existiert und ob sie nicht NULL ist
  • is_bool() - Prüft, ob eine Variable vom Typ boolean ist
  • is_numeric() - Prüft, ob eine Variable eine Zahl oder ein numerischer String ist
  • is_float() - Prüft, ob eine Variable vom Typ float ist
  • is_int() - Prüft, ob eine Variable vom Typ int ist
  • is_string() - Prüft, ob Variable vom Typ string ist
  • is_object() - Prüft, ob eine Variable vom Typ object ist
  • is_array() - Prüft, ob die Variable ein Array ist


10 BenutzerBeiträge:
- Beiträge aktualisieren...
claude dot pache at gmail dot com
9.05.2009 18:38
A small but important difference between "is_null" and "isset" is the following: "is_null" tests if an *expression* (not a *variable*) is null, while "isset" tests if a *variable* has null value or is undefined. The difference is manifested in the two following experiments:

Experiment 1:
<?php
function test() { return null; }
var_dump(is_null(test())); // displays "true"
var_dump(isset(test())); // parse error, because "test()" is not a variable
?>

Experiment 2:
<?php
error_reporting
(E_ALL);
var_dump(isset($an_undefined_variable)); // displays "false" since "$an_undefined_variable" is not defined
var_dump(is_null($an_undefined_variable)); // displays "true" (as expected), but throws a notice because "$an_undefined_variable" is not defined.
?>
george at fauxpanels dot com
1.12.2008 22:58
See how php parses different values. $var is the variable.

$var        =    NULL    ""    0    "0"    1

strlen($var)    =    0    0    1    1    1
is_null($var)    =    TRUE    FALSE    FALSE    FALSE    FALSE
$var == ""    =    TRUE    TRUE    TRUE    FALSE    FALSE
!$var        =    TRUE    TRUE    TRUE    TRUE    FALSE
!is_null($var)    =    FALSE    TRUE    TRUE    TRUE    TRUE
$var != ""    =    FALSE    FALSE    FALSE    TRUE    TRUE
$var        =    FALSE    FALSE    FALSE    FALSE    TRUE

Peace!
mdufour at gmail dot com
20.08.2008 6:31
Testing for a NULL field/column returned by a mySQL query.

Say you want to check if field/column “foo” from a given row of the table “bar” when returned by a mySQL query is null.
You just use the “is_null()” function:

[connect…]
$qResult=mysql_query("Select foo from bar;");
while ($qValues=mysql_fetch_assoc($qResult))
     if (is_null($qValues["foo"]))
         echo "No foo data!";
     else
         echo "Foo data=".$qValues["foo"];
[…]
Malfist
1.07.2008 16:54
Micro optimization isn't worth it.

You had to do it ten million times to notice a difference, a little more than 2 seconds

$a===NULL; Took: 1.2424390316s
 is_null($a); Took: 3.70693397522s

difference = 2.46449494362
difference/10,000,000 = 0.000000246449494362

The execution time difference between ===NULL and is_null is less than 250 nanoseconds. Go optimize something that matters.
strrev xc.noxeh@ellij
3.06.2008 14:42
$var===NULL is much faster than is_null($var) (with the same result)

I did some benchmarking with 10 million iterations:

$a=null;
 isset($a); Took: 1.71841216087s
 $a==NULL; Took: 1.27205181122s
 $a===NULL; Took: 1.2424390316s
 is_null($a); Took: 3.70693397522s
$a=5;
 isset($a); Took: 1.15165400505s
 $a==NULL; Took: 1.41901302338s
 $a===NULL; Took: 1.21655392647s
 is_null($a); Took: 3.78501200676s
error_reporting(E_ALL&~E_NOTICE);
unset($a);
 isset($a); Took: 1.51441502571s
 $a==NULL; Took: 16.5414860249s
 $a===NULL; Took: 16.1273870468s
 is_null($a); Took: 23.1918480396s

Please note, that isset is only included because it gives a good performance in any case; HOWEVER isset is NOT the same, or the opposite.
But you might be able to use isset() instead of null-checking.

You should not use is_null, except when you need a callback-function, or for conformity with is_int, is_float, etc.
ai dot unstmann at combase dot de
14.01.2008 21:17
For what I realized is that  is_null($var)  returns exactly the opposite of  isset($var) , except that is_null($var) throws a notice if $var hasn't been set yet.

the following will prove that:

<?php

$quirks
= array(null, true, false, 0, 1, '', "\0", "unset");

foreach(
$quirks as $var) {
    if (
$var === "unset") unset($var);

    echo
is_null($var) ? 1 : 0;
    echo isset(
$var) ? 1 : 0;
    echo
"\n";
}

?>

this will print out something like:

10    // null
01    // true
01    // false
01    // 0
01    // 1
01    // ''
01    // "\0"
Notice:  Undefined variable: var in /srv/www/htdocs/sandbox/null/nulltest.php on line 8
10    // (unset)

For the major quirky types/values is_null($var) obviously always returns the opposite of isset($var), and the notice clearly points out the faulty line with the is_null() statement. You might want to examine the return value of those functions in detail, but since both are specified to return boolean types there should be no doubt.

A second look into the PHP specs tells that is_null() checks whether a value is null or not. So, you may pass any VALUE to it, eg. the result of a function.
isset() on the other hand is supposed to check for a VARIABLE's existence, which makes it a language construct rather than a function. Its sole porpuse lies in that checking. Passing anything else will result in an error.

Knowing that, allows us to draw the following unlikely conclusion:

isset() as a language construct is way faster, more reliable and powerful than is_null() and should be prefered over is_null(), except for when you're directly passing a function's result, which is considered bad programming practice anyways.
powderz at gmail dot com
12.08.2006 6: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;
}
?>
MARSIK
31.07.2005 16: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 =)
michael at cannonbose dot com
30.12.2003 18: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
uioreanu at hotmail dot com
23.03.2001 15: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.]



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)