note 68825 deleted from function.is-null by danbrown

From: Date: Mon, 15 Jun 2009 12:20:36 +0000
Subject: note 68825 deleted from function.is-null by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-156877@lists.php.net to get a copy of this message
Note Submitter: powderz at gmail dot com ---- 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; } ?>

« previous php.notes (#156877) next »