Re: distinguish between null variable and unset variable

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Jan 22, 2009 at 12:21 AM, Paul M Foster <paulf@xxxxxxxxxxxxxxxxx>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@xxxxxxxxxxxx
http://Alexandre.Gaigalas.Net

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux