Results 1 to 8 of 8

Thread: $_POST not empty

  1. #1
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default $_POST not empty

    Because I was posting a page to itself
    I would first check to see if the item was set...

    if (isset($_POST['iquote'])){
    }

    to keep the function from running when the page is refreshed I was told to...
    if (isset($_POST['iquote])){

    header('Location: '.$_SERVER['PHP_SELF'].'?iquote='.$iquote);
    }

    How do you handle it if someone clicks the submit button without entering any data?

    if (isset($_POST['iquote'])) && $_POST['iquote'] <> empty(){
    Last edited by I am Abby; 04-26-2010 at 05:08 PM. Reason: typo in header

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    PHP Code:
    if( isset($_POST['iquote']) && !empty($_POST['iquote']) ){ /* do something */ 

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    traq, is that needed? Isn't (!empty($x)===isset($x))? At least I believe that empty($x) will never return an error even if $x is not set.
    I've never been sure about how PHP deals with variables that aren't set, but I've always used this:
    if (isset($x)&&$x!='') {....
    I've never had problems with it.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Apr 2010
    Location
    University of Illinois
    Posts
    86
    Thanks
    13
    Thanked 2 Times in 2 Posts

    Default

    Both ways seem to give the same results. Thanks boys!

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    It appears that empty() is better than what I use.
    if (!empty($x)) {...} should be all you need.
    It checks both if there is a value and that that value is not 'blank' (empty string, 0, etc.).
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by djr33 View Post
    traq, is that needed? Isn't (!empty($x)===isset($x))?
    ...
    if (isset($x)&&$x!='') {....
    ...yes and no...
    in most cases, they'll return the same answer. But (as I suspect you found below) a variable can be set (isset() returns TRUE) but be considered empty (e.g., an empty string - empty() returns TRUE).

    Incidentally, $x != '' is also generally equivalent to !empty(), but:
    Quote Originally Posted by php.net
    The following things are considered to be empty:
    * "" (an empty string)
    * 0 (0 as an integer)
    * "0" (0 as a string)
    * NULL
    * FALSE
    * array() (an empty array)
    * var $var; (a variable declared, but without a value in a class)
    Quote Originally Posted by djr33 View Post
    It appears that empty() is better than what I use.
    if (!empty($x)) {...} should be all you need.
    It checks both if there is a value and that that value is not 'blank' (empty string, 0, etc.).
    I don't know for sure if empty() makes isset() unneeded... for example, php.net says "...no warning is generated when the variable is not set", however, I don't know if that means empty differentiates between the two states. I suppose it's "close enough" in almost all cases (I can't quite think of an instance where it would matter, maybe if you're trying to do something with list() or extract()...).

    Anyway, yeah.

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    If the variable is not set, empty() returns TRUE; if the variable is set to any of the 'blank' values it also returns TRUE; otherwise it's false.
    I don't think there's any case whatsoever where this is not the desired output or where it would be aided by isset().
    Again, I've been doing it the long way for a while and always thought empty() was just a negative alias for isset(), so I'm glad I've now figured that out.


    empty() is also closer to the correct value than $x!='' because the != operator DOES ignore type so that:
    * "" (an empty string)
    * 0 (0 as an integer)
    * "0" (0 as a string)
    * NULL
    * FALSE
    all are considered matches.
    BUT it would not match an empty array.

    However, an empty array is in some sense "set" so that's possible.

    I expect an existing but not-set variable ( var $var; ) would return a match using !='' also.



    Note: to specify a certain type, use !==, so !=='' means "not exactly an empty string" and !==FALSE means "not exactly false" and !==0 means "not exactly a number equal to 0" etc. But with only != it ignores type. The 2 character operator (!= or ==) is type-ignoring and the 3 character operator (!== or ===) is type-specific.
    Last edited by djr33; 04-27-2010 at 04:27 AM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by djr33 View Post
    ...
    I don't think there's any case whatsoever where this is not the desired output or where it would be aided by isset().
    I can't think of anything either, but it still sticks in my head as significant for some reason.

    In any case, yes, I agree that if(!empty($x)) is fine in all practical respects, and really doesn't need the isset() check alongside it.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •