How to get the complete URL query string in PHP?

We are here again to learn another new thing in PHP. In this post, we are going to see how to get the complete query from the URL as the string. After getting it we will print it on the web page.

Before we are going to retrieve the URL query string, let’s see what is URL query.

To understand first take an URL as the demo:

The above URL contains the query “city=Mountain+View&state=California”. The query string of an URL always come after a question mark (?). That means, to send data via URL get method the query needs to send after a question mark in the URL.

Get random word from English dictionary in PHP tutorial

Get the image size (height and width) in PHP

So getting the query string from our demo URL means to get the “city=Mountain+View&state=California”. Now we are going to see how to get it.

PHP has a global variable $_SERVER which contains several pieces of information as the array. It also contains the query string of the URL in an index of the array. $_SERVER[‘QUERY_STRING’]  will return the query string. This should be on the page which contains the query in its URL so that it returns the query string. Below is the code that will print the URL query on the web page:


You can also echo it just like below:


It will simply return the URL query string. So it will display the below query:


So you can see that we have successfully retrieved the query string from the URL and print it on the web page.

Leave a Reply

Your email address will not be published. Required fields are marked *