Re: distinguish between null variable and unset variable
| From: | Alexandre Gaigalas | Date: | Thu, 22 Jan 2009 02:26:29 +0000 |
| Subject: | Re: distinguish between null variable and unset variable | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-286986@lists.php.net to get a copy of this message | ||
On Thu, Jan 22, 2009 at 12:21 AM, Paul M Foster <paulf@quillandmouse.com>wrote:
> 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
>
I've replaced all my techniques for testing POST and GET data by the filter
extension.
http://php.net/filter
--
Alexandre Gomes Gaigalas
alexandre@gaigalas.net
http://Alexandre.Gaigalas.Net