You are here: Re: isset(), undefined variables, and null « PHP Programming Language « IT news, forums, messages
Re: isset(), undefined variables, and null

Posted by Norman Peelman on 07/04/07 05:59

arundelo@hotmail.com wrote:
> Norm:
>
>> No... !isset($Foo) determines if $Foo is ok to evaluate.
>
> When I say "safe to evaluate", I mean it in exactly the
> sense of the the first sentence of the original post:
>
>> Is there a way to tell whether accessing a variable will
>> result in an "Undefined variable" E_NOTICE?
>
> Norm:
>
>> $dump = array_key_exists('Foo',$GLOBALS) ? var_dump($Foo)
>> : 'Not safe to evaluate.';
>>
>> should be what you're looking for, No?
>
> Doesn't work if $Foo is local. The get_defined_vars()
> solution upthread does exactly what I what I was seeking,
> despite its kluginess. So does your error_get_last() idea
> mentioned downthread (this is a variation on my solutions
> from the original post).
>
>> I'd like to see how your using this...
>
> Oh, I'm not using it; I just realized that isset()'s
> behavior is, IMHO, broken, and I was curious if there was
> something that did what I thought isset() should do. (In
> the actual situation that inspired my curiosity, I wanted to
> know whether it was safe to evaluate $Foo['Bar'], but
> array_key_exists('Bar', $Foo) works fine for that.)
>
> Thanks,
>
> --
> Aaron
> http://arundelo.com/
>

Ok, so here we go:

$dump = array_key_exists('Foo',get_defined_vars()) ? var_dump($Foo) :
'Variable is not defined!';

....but, can't figure out how to pass in an array.

How would you find 'Foo' given $Foo? To provide any real usefulness
you would need to be able to try any variable... which in itself is a
problem since you would have to pass an untested variable to test.

function is_var_defined(@mixed)
{
// return TRUE or FALSE based on whether variable has been
// defined in the name-space regardless of value or NULL
}


The problem is that, the function 'defined()' should be expanded to
encompass any variable not just constants. The bigger problem is that
(afaik) this is kludgey at its best as you may not always know what
variable you are checking against. I would imagine the best way to do
this is programatically:

if (@$Foo === NULL)
{
echo "Variable is not set<br>";
$Foo = 'Bar';
}
else
{
echo "Variable is set<br>";
}

-------------

//$Foo = 'I'm not set!';
echo var_dump(@$Foo); // output is NULL
$Foo = NULL;
echo var_dump(@$Foo); // output is NULL

So, actually we are back to square one with:

$dump = isset($Foo) ? var_dump($Foo) : 'Not readable';

I go back to my original statement. NULL is a placeholder, not a
value. Generally speaking, NULLs aren't something you normally 'process'
since NULL by definition means there is nothing there (not even 0).
Whether the said variable exists within the namespace or not is
unimportant. That being said, isset() works as expected. My question to
you is what is the importance of checking for the error if NULL is NULL?

Norm

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация