Checking for null and missing query string parameters in PHP

Yarin picture Yarin · Dec 8, 2010 · Viewed 55.9k times · Source

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

Answer

Byron Whitlock picture Byron Whitlock · Dec 8, 2010

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