How to use PHP $_GET Global Variable

In this guide, we will take you through the basics of using the PHP $_GET global variable for handling incoming GET data.

How to use PHP $_GET Global Variable

In PHP, $_GET is a super global variable that is an associative array of variables. This variable contains data that has been sent using the HTTP GET method. For example, a GET request is sent as a URL and can contain a query string that has name-value pairs.

As GET request data is available in the URL, using it for sensitive data is not recommended as it can be easily viewed, bookmarked, and cached. Instead, it is recommended that you use a POST request for data that is considered sensitive.

There are many uses for GET, but it works best when used as a query. For example, a search query, read operation, data lookup, or similar. Using the query string is also a popular way of defining tracking codes.

An alternative to using $_GET is the $_POST super global variable. Using the $_POST variable you can access data sent using an HTTP POST request. For example, you should use a POST request for data such as eCommerce orders, logins, and comment fields.

This tutorial will explain the basics of an HTTP GET request and how you can use the $_GET super global variable in PHP to process a GET request. All these concepts are vital to developing a web application that uses PHP.

What is the HTTP GET Method

The GET method uses HTTP (Hypertext Transfer Protocol) to send a request to a server. Depending on the data within the request, the server will send a response to the client. The two most common types of an HTTP request is GET and POST.

A GET request is sent as a URL and can contain a query string that has name-value pairs.

The query string is the data after the ? in the URL. If you wish to define multiple name=value pairs, you will need to use & after each pair.


There are several things that you need to know in regards to GET requests.

  • Browser limits restrict a GET request’s length, roughly 2kb to 8kb, depending on the browser. The receiving server can also set a limit.
  • You can bookmark GET requests in a browser.
  • A GET request can be cached.
  • You can find GET requests in the browser history.
  • GET requests should not be used with sensitive data as it can easily be viewed, cached, or even bookmarked.

You can view headers sent with a request in the browser developer tools network tab. You can see the type of request in the HTTP headers by looking for the method header. The example below has the method of GET.

:authority: development.local
:method: GET
:path: /get.php
:scheme: https
accept: next/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9

Example of using $_GET in PHP

We can use the $_GET super global variable in PHP to process the name-value pairs that make up the optional query string. Also, you can use the $_GET variable in any scope in your PHP script as it is a global variable.

We have an example script to demonstrate how to use the $_GET variable. We will quickly explain the code below.

Firstly, we have a PHP code section where we create two variables $bg_color and $text. For each variable, we assign a value from our $_GET variable. Since our $_GET variables can be user input, we run the value through the htmlspecialchars function before using it.

Lastly, we have some HTML in which we output the value of our two variables. We echo the color value in our HTML body tag for the page’s background color. Also, for the text, we echo the value of the text variable inside H1 HTML tags.

It is a simple PHP script but should hopefully explain the basics of using PHP to access the URL query string name-value pairs.


$bg_color = htmlspecialchars($_GET['bg']);
$text = htmlspecialchars($_GET['text']);


  <body style="background: <?php echo $bg_color ?>">
    <h1><?php echo $text ?></h1>

Below is the URL that we will send to the above PHP script. We named our script get.php and stored it on an accessible local development server.

Our query string has bg=beige where name is bg and value is beige. You can access the value by using the name in our GET variable, for example, $_GET['bg'].

It is the same logic with text=Hello%20World, where the value is accessible using the name, for example, $_GET['text']. Also, make note we have an ampersand (&) separating our name-value pairs, you will need to do this for every additional name-value pair.


We get the following output by entering the URL above into our browser. The background is a beige color, and the title is “Hello World”. Changing either of these values in our URL will alter the output of our page.

PHP GET Super Global Variable Example


I hope by the end of this tutorial, you understand the basics of a GET request and how you can use $_GET to process the query string in PHP. You will likely use GET requests when it comes to most types of web development.

We have plenty of other PHP tutorials that will take you through many more basics. For example, if you are a beginner, I recommend learning more about for loops, if-else statements, and variables.

Please let us know if you notice a mistake or an important topic is missing from this guide.

Leave a Reply

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