467,142 Members | 1,388 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,142 developers. It's quick & easy.

problem with isset()

I'm really confused with something I never had any problems with
before... I have a form with a textbox called password. On the second
page that $password is posted to, if I test isset($password) after a
submit, it always evaluates to true, regardless of whether or not
anything was entered into the textbox... echoing the value simply echoes
its contents, which is an empty string. If I test for simply
if($password), however, that does evaluate to false. This doesn't work
in all cases, however, since sometimes it will give an error saying the
variable is not defined (cases in which I've always used isset to test).
I read up on isset() on php.net, and it says that it will return false
if a variable has been set to null... is that not the case here? If
anyone could tell me exactly what type of situation calls for each of
these two methods I would really appreciate it, thanks in advance.

Marcus

Jul 16 '05 #1
  • viewed: 6134
Share:
3 Replies
RG

"Marcus" <Ju********@aol.com> wrote in message
news:3F**************@aol.com...
I'm really confused with something I never had any problems with
before... I have a form with a textbox called password. On the second
page that $password is posted to, if I test isset($password) after a
submit, it always evaluates to true, regardless of whether or not
anything was entered into the textbox... echoing the value simply echoes
its contents, which is an empty string. If I test for simply
if($password), however, that does evaluate to false. This doesn't work
in all cases, however, since sometimes it will give an error saying the
variable is not defined (cases in which I've always used isset to test).
I read up on isset() on php.net, and it says that it will return false
if a variable has been set to null... is that not the case here? If
anyone could tell me exactly what type of situation calls for each of
these two methods I would really appreciate it, thanks in advance.

Marcus


Probably something to do with globals, sound like they are on and variables
are alive longer.
RG
Jul 16 '05 #2
"Marcus" <Ju********@aol.com> wrote in message
news:3F**************@aol.com...
[snip]
submit, it always evaluates to true, regardless of whether or not
anything was entered into the textbox... echoing the value simply echoes
its contents, which is an empty string. If I test for simply
if($password), however, that does evaluate to false. This doesn't work
Yes, empty string is considered false by PHP
in all cases, however, since sometimes it will give an error saying the
variable is not defined (cases in which I've always used isset to test).
I read up on isset() on php.net, and it says that it will return false
if a variable has been set to null... is that not the case here? If
No, the variable is not undefined, the empty string is still a value, it is
a string with no characters in it, same way that 0 (zero) is a number, not
an undefined integer.

So,
$password = '';
if ( isset($password) ) print "is set";
if ( $password ) print "is non-empty"; else print "is empty";

will print :
is set
is empty

Cos $password is defined, and it has a value, the empty string.
And cos the string has no characters in it, it is considered false, hence
prints "is empty"
anyone could tell me exactly what type of situation calls for each of
these two methods I would really appreciate it, thanks in advance.

Marcus


Thanks,
Mark
---------------------------------------------------------------------------
Windows, Linux and Internet Development Consultant
Email: co*******@scriptsmiths.com
Web: http://www.scriptsmiths.com
---------------------------------------------------------------------------
Jul 16 '05 #3
> page that $password is posted to, if I test isset($password) after a
submit, it always evaluates to true, regardless of whether or not
anything was entered into the textbox... echoing the value simply echoes
its contents, which is an empty string. If I test for simply
if($password), however, that does evaluate to false. This doesn't work


Usually what I do when testing things like this is to first check if the
variable exists, and then check its value. This way you don't get the error
if you are trying to test the value of something which doesn't exist, but if
it does exist you are still checking its value as you usually would. ie:

if(isset(x))
{
if(x!=0 && x!=NULL && x!= '')
{
<password is set - test password value>
}
else <blank password>
}
else <no password>
David
Jul 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by newbie_mw | last post: by
reply views Thread by newbie_mw | last post: by
9 posts views Thread by wouter | last post: by
2 posts views Thread by Event Horizon | last post: by
9 posts views Thread by arundelo@hotmail.com | last post: by
10 posts views Thread by SoulIntruder | last post: by
5 posts views Thread by rhodarose | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.