24

I want to be able to distinguish between existing query string parameters set to null, and missing parameters. So the parts of the question are:

  • How do I check if a parameter exists in the query string
  • What's the established method for passing a null value in a query string? (e.g. param=null or param=(nothing) )

Thanks

Yarin
  • 173,523
  • 149
  • 402
  • 512

4 Answers4

39

Use isset() and empty()

if (isset($_REQUEST['param']))
{
  // param was set in the query string
   if(empty($_REQUEST['param']))
   {
     // query string had param set to nothing ie ?param=&param2=something
   }
}
Byron Whitlock
  • 52,691
  • 28
  • 123
  • 168
  • 3
    +1! Correct answer, but you do not need `isset()` when using `empty()`, since `empty()` implies `isset()`. This is especially relevant when using `!empty()`. And you should probably add an explicit answer to the second part of the question. – jwueller Dec 07 '10 at 23:20
  • @elusive, If you call empty on a variable that isn't set, you will get an E_WARNING. – Byron Whitlock Dec 07 '10 at 23:21
  • 2
    @Byron Whitlock: I think that is not correct. The docs state _"empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set."_. – jwueller Dec 07 '10 at 23:23
  • @Byron `empty()` does **not** produce warnings when the variable is not set. – zzzzBov Dec 07 '10 at 23:29
  • 1
    I stand corrected! Empty indeed doesn't thow warnings on unset vars. Must be a throwback to when I learned php in the 3.0 days ;P I learn somthing new everyday on SO. thanks @elusive,@zzzzBov ! – Byron Whitlock Dec 07 '10 at 23:33
  • 11
    $_REQUEST is a combination of $_GET, $_POST, and $_COOKIE and should rarely, if ever, be used because of the security implications of going to it as opposed to the actual source of your variable. This question concerns query string parameters so $_GET should be used. – Paolo Bergantino Dec 07 '10 at 23:48
13

Or use array_key_exists:

if(array_key_exists("myParam", $_GET)) {

}

I've never been keen on 'conventions' for passing empty values to the server - I'm used to testing for the presence of variables, and then trimming them and testing for emptiness, for example.

karim79
  • 339,989
  • 67
  • 413
  • 406
4

Values stored in $_GET and $_POST can only be strings or arrays, unless explicitly set at run-time. If you have a query string of query=string the value is "string" if you instead use: query=null the value will be "null". Note that it is therefor a string.

If you send: query=, the value will be "" or the empty string. Take note of the differences between isset and empty. isset will be true if the value is not null, whereas empty will be true when the value evaluates to false. Therefor "" will be true for both isset and empty.

If you just want to check if a query string parameter was set to the string value of "null", you can simply check $_GET['query']=='null' (you may want to adjust the case of the characters before the check)

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
2

With one if statement instead of two:

if ((isset($_REQUEST['name'])) && (!empty($_REQUEST['name'])))
{
    $name= $_REQUEST['name'];
}
MatthewK
  • 466
  • 4
  • 9
  • 19