Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,057
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Handling Empty Parameter in Query String

    If a parameter in your Query String is empty, should you drop the parameter or leave it in?

    For example...

    Parameter exists:
    Code:
    www.debbie.com/books/php?filter=hard-cover&sort=title&sortdir=asc

    Parameter does NOT exist: (Option #1)
    Code:
    www.debbie.com/books/php?filter=&sort=title&sortdir=asc

    Parameter does NOT exist: (Option #2)
    Code:
    www.debbie.com/books/php?sort=title&sortdir=asc
    Sincerely,


    Debbie

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,237
    Thanks
    12
    Thanked 340 Times in 336 Posts
    I’d make it dependend upon whether there is a default value for the parameter and if that default value triggers something (a default filter certainly does more than no filter at all)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,299
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    From my POV Deb, it makes no difference if you have it there or not. It's how you test for its existence in your php that matters.

    As long as you do something like this, you'll be ok whatever you do with the url:

    PHP Code:
    if ((isset($_GET['filter'])) and ($_GET['filter'] != ''))
    do_something(); } 
    Alternatively using a ternary, you can take the value if it exists or use a default:

    PHP Code:
    $Filter = ((isset($_GET['filter'])) and ($_GET['filter'] != ''))? $_GET['filter']: 'default_value'
    The latter is something I use frequently in my code.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #4
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,057
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    From my POV Deb, it makes no difference if you have it there or not. It's how you test for its existence in your php that matters.

    As long as you do something like this, you'll be ok whatever you do with the url:

    PHP Code:
    if ((isset($_GET['filter'])) and ($_GET['filter'] != ''))
    do_something(); } 
    Alternatively using a ternary, you can take the value if it exists or use a default:

    PHP Code:
    $Filter = ((isset($_GET['filter'])) and ($_GET['filter'] != ''))? $_GET['filter']: 'default_value'
    The latter is something I use frequently in my code.
    Yes, I'm neurotic about data checking and sanitizing, so no worries there.

    I suppose the URL looks cleaner if you don't have unused parameters in it, but IT geeks probably don't care.

    I guess I will just leave the check for $_GET['filte'] in my code, but when I build my URL, not add filter= if the user didn't actually choose a filter.

    Thanks,


    Debbie


  •  

    Posting Permissions

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