By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,024 Members | 2,027 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,024 IT Pros & Developers. It's quick & easy.

isset(), undefined variables, and null

P: n/a
Is there a way to tell whether accessing a variable will
result in an "Undefined variable" E_NOTICE? isset() almost
does this, but it also returns false if a variable is set to
null:

$SetNonNull = 0;
$SetNull = null;
var_dump(isset($SetNonNull)); // bool(true)
$Junk = $SetNonNull; // No error, as predicted by isset().
var_dump(isset($NotSet)); // bool(false)
$Junk = $NotSet; // Raises an E_NOTICE, as predicted by isset().
var_dump(isset($SetNull)); // bool(false)
$Junk = $SetNull; // No error -- how to predict?

I realize I could do this by e.g., doing @$Var and checking
afterwards using $php_errormsg or a custom error handler;
I'm hoping for something less klugey.

Thanks,

--
Aaron
http://arundelo.com/

Jun 29 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
ar******@hotmail.com wrote:
Is there a way to tell whether accessing a variable will
result in an "Undefined variable" E_NOTICE? isset() almost
does this, but it also returns false if a variable is set to
null:
You could use is_null().
--
Andrew Hutchings - LinuxJedi - http://www.linuxjedi.co.uk/
Windows is the path to the darkside...Windows leads to Blue Screen. Blue
Screen leads to downtime. Downtime leads to suffering...I sense much
Windows in you...
Jun 29 '07 #2

P: n/a
ar******@hotmail.com wrote:
Is there a way to tell whether accessing a variable will
result in an "Undefined variable" E_NOTICE? isset() almost
does this, but it also returns false if a variable is set to
null:

$SetNonNull = 0;
$SetNull = null;
var_dump(isset($SetNonNull)); // bool(true)
$Junk = $SetNonNull; // No error, as predicted by isset().
var_dump(isset($NotSet)); // bool(false)
$Junk = $NotSet; // Raises an E_NOTICE, as predicted by isset().
var_dump(isset($SetNull)); // bool(false)
$Junk = $SetNull; // No error -- how to predict?

I realize I could do this by e.g., doing @$Var and checking
afterwards using $php_errormsg or a custom error handler;
I'm hoping for something less klugey.

Thanks,

--
Aaron
http://arundelo.com/
I'm afraid you're out of luck with it. You could also inspect the symbol
table directly via get_defined_vars, but that's ugly.

--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Jun 30 '07 #3

P: n/a
gosha bine wrote:
You could also inspect the symbol table directly via
get_defined_vars
Ah yes! (This also occurred to me after my original post.)

$Vars = get_defined_vars();
if (isset($Foo) || array_key_exists('Foo', $Vars)) {
// Safe to evaluate $Foo:
var_dump($Foo);
} else {
echo "Not safe to evaluate \$Foo.\n";
}
but that's ugly.
Indeed, but not quite as bad as my other solution.

Andrew Hutchings wrote:
You could use is_null().
Nope, is_null() always tries to evaluate its argument, so an
E_NOTICE might be emitted.

--
Aaron
http://arundelo.com/

Jul 1 '07 #4

P: n/a
ar******@hotmail.com wrote:
gosha bine wrote:
>You could also inspect the symbol table directly via
get_defined_vars

Ah yes! (This also occurred to me after my original post.)

$Vars = get_defined_vars();
if (isset($Foo) || array_key_exists('Foo', $Vars)) {
// Safe to evaluate $Foo:
var_dump($Foo);
} else {
echo "Not safe to evaluate \$Foo.\n";
}
>but that's ugly.

Indeed, but not quite as bad as my other solution.

Andrew Hutchings wrote:
>You could use is_null().

Nope, is_null() always tries to evaluate its argument, so an
E_NOTICE might be emitted.

--
Aaron
http://arundelo.com/
Easier:

$dump = (!isset($Foo) || $Foo === NULL) ? 'Not safe to evaluate' :
var_dump($Foo);
Norm
Jul 2 '07 #5

P: n/a
Norman Peelman wrote:
Easier:

$dump = (!isset($Foo) || $Foo === NULL) ? 'Not safe to evaluate' :
var_dump($Foo);
Some problems:

- The second operand of the ||-expression is redundant
because if !isset($Foo) is false then $Foo === NULL will
always be false (and vice versa, for that matter).
- Even if that weren't the case, $Foo === NULL evaluates
$Foo before it's determined whether it's safe to evaluate
$Foo. (That's easy to fix with the @ operator.)
- It gives an incorrect result if $Foo is safe to evaluate
but has had NULL assigned to it:

$Foo = NULL;
$dump = (!isset($Foo) || $Foo === NULL)
? 'Not safe to evaluate'
: var_dump($Foo); // Not reached.
echo "$dump\n"; // Prints "Not safe to evaluate" even though
// $Foo is safe to evaluate.

--
Aaron
http://arundelo.com/

Jul 2 '07 #6

P: n/a
ar******@hotmail.com wrote:
Norman Peelman wrote:
>Easier:

$dump = (!isset($Foo) || $Foo === NULL) ? 'Not safe to evaluate' :
var_dump($Foo);

Some problems:

- The second operand of the ||-expression is redundant
because if !isset($Foo) is false then $Foo === NULL will
always be false (and vice versa, for that matter).
- Even if that weren't the case, $Foo === NULL evaluates
$Foo before it's determined whether it's safe to evaluate
No... !isset($Foo) determines if $Foo is ok to evaluate. If $Foo is not
set or === NULL then $Foo === NULL never gets evaluated. But yes you are
correct in that that part is redundant. Which by the way should have
answered you question at this point. See below.
$Foo. (That's easy to fix with the @ operator.)
....that only suppresses the error.
- It gives an incorrect result if $Foo is safe to evaluate
but has had NULL assigned to it:
I assumed that NULL was not an acceptable value for what you were
looking for. Again, see below.
$Foo = NULL;
$dump = (!isset($Foo) || $Foo === NULL)
? 'Not safe to evaluate'
: var_dump($Foo); // Not reached.
echo "$dump\n"; // Prints "Not safe to evaluate" even though
// $Foo is safe to evaluate.

--
Aaron
http://arundelo.com/
Try this:

$dump = array_key_exists('Foo',$GLOBALS) ? var_dump($Foo) : 'Not safe to
evaluate.';

should be what you're looking for, No? I'd like to see how your using
this...

Norm
Jul 3 '07 #7

P: n/a
ar******@hotmail.com wrote:
Norman Peelman wrote:
>Easier:

$dump = (!isset($Foo) || $Foo === NULL) ? 'Not safe to evaluate' :
var_dump($Foo);

Some problems:

- The second operand of the ||-expression is redundant
because if !isset($Foo) is false then $Foo === NULL will
always be false (and vice versa, for that matter).
- Even if that weren't the case, $Foo === NULL evaluates
$Foo before it's determined whether it's safe to evaluate
$Foo. (That's easy to fix with the @ operator.)
- It gives an incorrect result if $Foo is safe to evaluate
but has had NULL assigned to it:

$Foo = NULL;
$dump = (!isset($Foo) || $Foo === NULL)
? 'Not safe to evaluate'
: var_dump($Foo); // Not reached.
echo "$dump\n"; // Prints "Not safe to evaluate" even though
// $Foo is safe to evaluate.

--
Aaron
http://arundelo.com/
Or take a look at:

http://us2.php.net/manual/en/functio...r-get-last.php

maybe what you're really looking for. For PHP >= 5.2.0

Norm
Jul 3 '07 #8

P: n/a
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/

Jul 3 '07 #9

P: n/a
ar******@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
Jul 4 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.