Best test for empty form value ?

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

    Best test for empty form value ?

    Hi guys,

    What's the best way to test for an empty form value ? I am doing it like
    this now:

    $test = $_POST['inputTest'];
    if(strlen($test ) < 1)
    // it is empty !

    Maybe I can just go:

    if($test == '') // that's two single-quotes with nothing in between
    // it is empty !

    At least I avoid a function call that way. Maybe there is a better way ?

    Take care,
    Cyrus


  • john.postlethwait@gmail.com

    #2
    Re: Best test for empty form value ?

    Either way should work find, the second way may have less load on the
    server, but honestly neither of them would cause too much... So
    whatever is your preference. I ususally do it the second way and never
    had a problem with it.

    Comment

    • Guest's Avatar

      #3
      Re: Best test for empty form value ?

      > What's the best way to test for an empty form value ? I am doing it like [color=blue]
      > this now:
      >
      > $test = $_POST['inputTest'];
      > if(strlen($test ) < 1)
      > // it is empty !
      >
      > Maybe I can just go:
      >
      > if($test == '') // that's two single-quotes with nothing in between
      > // it is empty ![/color]

      It has been my experience that the function "empty()" has not failed me.

      If you have a string, "" or '' or "0" is empty.
      If you have a numeric variable, 0 is empty
      Arrays with no elements is empty
      A class property that has been declared but not used (set) is empty
      A boolean variable that is FALSE is empty
      NULL values are empty
      Any object with no properties set is empty

      Determine whether a variable is empty


      _______________ _______________ ______
      Wil Moore III, MCP | Integrations Specialist | Assistant Webmaster

      Comment

      • Guest's Avatar

        #4
        Re: Best test for empty form value ?

        [color=blue]
        > $test = $_POST['inputTest'];
        > if(strlen($test ) < 1)
        > // it is empty !
        >
        > Maybe I can just go:
        >
        > if($test == '') // that's two single-quotes with nothing in between
        > // it is empty ![/color]

        Also, do not forget, if you are testing for a key in an array such as your example: $_POST['inputTest'];
        you can do isset($_POST['inputTest']) or you can use array_key_exist s('inputTest', $_POST)

        If you KNOW the key is there, but you just want to know if it has a NULL or "empty" value, your best bet is to run it through the empty() function.

        _______________ _______________ ______
        Wil Moore III, MCP | Integrations Specialist | Assistant Webmaster

        Comment

        • Tony Marston

          #5
          Re: Best test for empty form value ?

          The foolproof way to check for an empty string is always strlen($var) < 1.
          You may also want to use trim() to remove any leading/trailing spaces.

          --
          Tony Marston

          This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL




          "Cyrus D." <satan@invalid. org> wrote in message
          news:p7gdd.2387 6$YM4.6134341@n ews4.srv.hcvlny .cv.net...[color=blue]
          > Hi guys,
          >
          > What's the best way to test for an empty form value ? I am doing it like
          > this now:
          >
          > $test = $_POST['inputTest'];
          > if(strlen($test ) < 1)
          > // it is empty !
          >
          > Maybe I can just go:
          >
          > if($test == '') // that's two single-quotes with nothing in between
          > // it is empty !
          >
          > At least I avoid a function call that way. Maybe there is a better way ?
          >
          > Take care,
          > Cyrus
          >
          >[/color]


          Comment

          Working...