Re: distinguish between null variable and unset variable
| From: | Shawn McKenzie | Date: | Thu, 22 Jan 2009 20:11:19 +0000 |
| Subject: | Re: distinguish between null variable and unset variable | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-287028@lists.php.net to get a copy of this message | ||
Shawn McKenzie wrote:
> Daniel Brown wrote:
>> On Wed, Jan 21, 2009 at 20:27, Jack Bates <ms419@freezone.co.uk> wrote:
>>> How can I tell the difference between a variable whose value is null and
>>> a variable which is not set?
>> Unfortunately, in PHP - like other languages - you can't.
>>
>> A variable is considered to be null if:
>> * it has been assigned the constant NULL.
>> * it has not been set to any value yet.
>> * it has been unset().
>>
>
> I'm not in a position to test right now, but using Dan's logic I would
> turn it around and test for isset first and then is_null. This makes
> sense to me, but maybe it is flawed:
>
> if (isset($var) && is_null($var)) {
> echo "$var is set and is null";
> }
>
> Or maybe a function to return is the $var === null:
>
>
> function eq_null(&$var)
> {
> return (isset($var) && is_null($var)) ? true : false;
> }
>
Or something like this (dunno, just brainstorming):
function setornull(&$var)
{
if (!isset($var)) {
return false;
}
elseif (is_null($var)) {
return null;
}
return true;
}
--
Thanks!
-Shawn
http://www.spidean.com