Re: Null values
| From: | CC Zona | Date: | Thu, 20 Jun 2002 06:50:08 +0000 |
| Subject: | Re: Null values | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-103085@lists.php.net to get a copy of this message | ||
In article
<0D18940A29184D41AF9D369D0BD40D01829280@exch2k.emsg.darden.com>,
JDrabb@darden.com (James Drabb) wrote:
> What is the best way to check for a null value returned by a DB.
> Would something like if ($row['fldUserName'] != False) be reliable
> enough?
Checking it in the database is your best bet. For instance, MySQL's syntax
uses "is [not] null", i.e. "select foo from bar where bar is null". One
gotcha to watch out for if you decide to check later in the process: make
sure that the database result function you're using will return null
values. For example, mysql_fetch_row(), does not.
PHP's loose typing means that a null value will evaluate as false, but it's
up to you to decide whether other values that evaluate to false (including
zero values and empty strings) can be permissable values there too. My
preference is to code what I mean, so that later there's no chance that an
assumption that once seemed "implicit" suddenly is the source of a
seemingly baffling inconsistency.
If you want to check specifically for a null, use either "is_null()" or
"===null". If any false-evaluating value will do, use "== false". If only
boolean false is allowed, use "===false". For other occasions, there are
also the alternatives of empty() and !isset().
It's worthwhile to learn the differences between each of these methods.
The differences seem subtle or even trivial at first, but treating them as
interchangeable is a dangerous habit to get into.
--
CC