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

problem with isset()

P: n/a
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
Share this Question
Share on Google+
3 Replies


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

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

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