Re: distinguish between null variable and unset variable
| From: | Paul M Foster | Date: | Thu, 22 Jan 2009 02:21:45 +0000 |
| Subject: | Re: distinguish between null variable and unset variable | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-286985@lists.php.net to get a copy of this message | ||
On Wed, Jan 21, 2009 at 05:27:35PM -0800, Jack Bates wrote:
> How can I tell the difference between a variable whose value is null and
> a variable which is not set?
>
> // cannot use === null:
>
> ket% php -r '$null = null; var_dump(null === $null);'
> bool(true)
> ket% php -r 'var_dump(null === $unset);'
> bool(true)
> ket%
>
> // - cannot use isset() either:
>
> ket% php -r '$null = null; var_dump(isset($null));'
> bool(false)
> ket% php -r 'var_dump(isset($unset));'
> bool(false)
> ket%
Oh I *love* this problem. I still haven't found the perfect solution for
it. But since a lot of things in PHP float around as strings, I often
use strlen(trim($var)) == 0 to determine the "emptiness" of a variable.
But it all depends on what type of variable you expect to receive. I
don't have this problem so much with methods and functions, since I
specifically engineer them to give me exact results. But I get it when
testing POST and GET variables from web pages.
Paul
--
Paul M. Foster