problem with isset()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marcus

    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

  • RG

    #2
    Re: problem with isset()


    "Marcus" <JumpMan222@aol .com> wrote in message
    news:3F0134F5.2 040701@aol.com. ..[color=blue]
    > 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
    >[/color]

    Probably something to do with globals, sound like they are on and variables
    are alive longer.
    RG


    Comment

    • Mark Hewitt

      #3
      Re: problem with isset()

      "Marcus" <JumpMan222@aol .com> wrote in message
      news:3F0134F5.2 040701@aol.com. ..
      [snip]
      [color=blue]
      > 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[/color]

      Yes, empty string is considered false by PHP
      [color=blue]
      > 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[/color]

      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"
      [color=blue]
      > 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
      >[/color]

      Thanks,
      Mark
      ---------------------------------------------------------------------------
      Windows, Linux and Internet Development Consultant
      Email: corporate@scrip tsmiths.com
      Web: http://www.scriptsmiths.com
      ---------------------------------------------------------------------------


      Comment

      • David Walker

        #4
        Re: problem with isset()

        > page that $password is posted to, if I test isset($password ) after a[color=blue]
        > 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[/color]

        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


        Comment

        Working...