Can anyone help me with a problem. It should be simple but I can't work out a solution.
I need to be able to see if a variable is defined, even if its value is null.
You can't use isSet() as it gives false after you set the variable to null as follows.
$a = null
isSet($a) give false, but the variable exists, it just has a null value, Subsequent use of $a in function calls will be fine.
You can't use is_null() as it gets an error when the variable has never been defined.
I hope this makes sense,and that somebody can help
How can I tell if a variable has been defined?
Moderator: General Moderators
-
gregambrose
- Forum Newbie
- Posts: 12
- Joined: Thu Aug 07, 2003 11:01 pm
- Location: Sydney, Australia
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: How can I tell if a variable has been defined?
It's ugly, but you could maybe use the error supression operator:
Code: Select all
if (! isSet($a) && ! @is_null($a)) {(#10850)
-
gregambrose
- Forum Newbie
- Posts: 12
- Joined: Thu Aug 07, 2003 11:01 pm
- Location: Sydney, Australia
Re: How can I tell if a variable has been defined?
Thanks for the note. The trouble is is_null() returns true on an undefined variable.
In the following, where $test has never been used
if(! isSet($test) && ! @is_null($test)) { echo 'undefined'; } else echo "was defined";
We get the following result
was defined
Notice: Undefined variable: test in C:\Docu......
Not sure why the @ is being disregarded, but that doesn't change the problem.
In the following, where $test has never been used
if(! isSet($test) && ! @is_null($test)) { echo 'undefined'; } else echo "was defined";
We get the following result
was defined
Notice: Undefined variable: test in C:\Docu......
Not sure why the @ is being disregarded, but that doesn't change the problem.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: How can I tell if a variable has been defined?
Did you try other combinations and chained if()s ? Like:
I think the solution would be of interest to people here...
Code: Select all
if(! isSet($test) && @is_null($test)) {
} elseif (! isset($a) ) {
...(#10850)
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
Re: How can I tell if a variable has been defined?
For sure it is - we could write real_isset() and warn people never to use the legacy isset() function instead of fixing it.
Whoa... deja-vu!
Whoa... deja-vu!
-
gregambrose
- Forum Newbie
- Posts: 12
- Joined: Thu Aug 07, 2003 11:01 pm
- Location: Sydney, Australia
Re: How can I tell if a variable has been defined?
I had a reply on this problem from the Sydney PHP Group , Precariouspanther, who came up with the following. Rather ugly but it works.
$a=null;
if(array_key_exists("a",get_defined_vars())){
echo "$a Has been defined or is null";
}
I'll wrap this in a function and add it to my library, even though its not very elegant. You would think PHP would have a function variableDefined() like defined() for constants.
I've tried all combinations of if statements on isSet(), empty() and is_null() (the only relevant functions I think), and can't get a solution.
$a=null;
if(array_key_exists("a",get_defined_vars())){
echo "$a Has been defined or is null";
}
I'll wrap this in a function and add it to my library, even though its not very elegant. You would think PHP would have a function variableDefined() like defined() for constants.
I've tried all combinations of if statements on isSet(), empty() and is_null() (the only relevant functions I think), and can't get a solution.