Search code examples
phprequestquery-stringquerystringparameter

Checking for null and missing query string parameters in PHP


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


Solution

  • 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
       }
    }