How to get Query String in PHP?

April 30, 2008

Recently I was implementing a very basic paging system in PHP. The system also supported search functionality, where search parameter was passed in query string, URL was looking like this:

contactlist.php?searchText=some_search_text&pageno=2

contactlist.php or

contactlist.php?pageno=3

The requirement was quite easy I need to append page number to current query string. For those who do not know what is a query string, a small explanation. Query string is used to pass variables. Let’s look at this url:

contactlist.php?searchText=some_search_text&pageno=2

contactlist.php is a script name

 Everything that comes after “?” is a query string, so in this example it will be searchText=some_search_text&pageno=2. searchText and pageno are variables and some_search_text and 2 are values. Query string has this form variable1=value&variable2=value, where each variable is separated by ampersand character (”&”). 

In PHP we can access query string using superglobal variableRecently I was implementing a very basic paging system in PHP. The system also supported search functionality, where search parameter was passed in query string, URL was looking like this:

contactlist.php?searchText=some_search_text&pageno=2

contactlist.php or

contactlist.php?pageno=3

The requirement was quite easy I need to append page number to current query string. For those who do not know what is a query string, a small explanation. Query string is used to pass variables. Let’s look at this url:

contactlist.php?searchText=some_search_text&pageno=2

contactlist.php is a script name

SERVER, which is an array that contains many useful information. To access query string, use below code:

Recently I was implementing a very basic paging system in PHP. The system also supported search functionality, where search parameter was passed in query string, URL was looking like this:

contactlist.php?searchText=some_search_text&pageno=2

contactlist.php or

contactlist.php?pageno=3

The requirement was quite easy I need to append page number to current query string. For those who do not know what is a query string, a small explanation. Query string is used to pass variables. Let’s look at this url:

contactlist.php?searchText=some_search_text&pageno=2

contactlist.php is a script name

Everything that comes after “?” is a query string, so in this example it will be searchText=some_search_text&pageno=2. searchText and pageno are variables and some_search_text and 2 are values. Query string has this form variable1=value&variable2=value, where each variable is separated by ampersand character (“&”).

In PHP we can access query string using superglobal variable $_SERVER, which is an array that contains many useful information. To access query string, use below code:

$_SERVER['QUERY_STRING']

We should be very careful when appending new variables to query string, because as I showed above, some time query string can be empty and sometimes it can already contains some variables, because of this before appending new variable, we should check if we have to append ampersand character (“&”) before or not. This can be done by simple if statement presented below. Below code example also uses $_SERVER["PHP_SELF"], which returns script name

< ?php
   $self = $_SERVER["PHP_SELF"];
 
  if($_SERVER["QUERY_STRING"]) {
 
    $finalurl = $self . "?" . $_SERVER["QUERY_STRING"] . 
      "&myvariable=myvalue";   
 
  } else {
 
    $finalurl = $self . "?" . "myvariable=myvalue";  
 
  } 
?>

 

I hope this simple example will help some one.