How to use the query string in PHP

This article tells you how to pass variables to a PHP page using the query string, and how to access them from that page.

Introduction

Have you ever seen a URL which looked like "www.example.com/page.php?mode=1&style=red"? Well, this page is being passed variables and their values through the query string, here the variables "mode" and "style" are being passed, with values "1" and "red" respectively. The question mark indicates the start of the query string and the ampersand, &, symbol seperates variable=value assignments. The page is then able to read these variables and react according to them, for example to display them to the user.

Passing a query string to a page

There are two ways to pass a QueryString to a page, the first is to enter it manually, for example:

HTML:
  1. <a href="page.php?mode=1">Mode 1</a>

The above HTML creates a link to a page passing the variable mode with the value "1".

An alternative, and perhaps more useful, way is to use HTML forms. The main thing to remember with forms is that you need to use the GET method to send information via the query string, for example:

HTML:
  1. <form method="GET" action="page.php">
  2. Please enter your name: <input type="text" name="username" />
  3. <input type="submit" value="submit" />
  4. </form>

This code displays a simple HTML form with a text box and a submit button. When the user enters their name and presses submit, the information in the name box is passed to a page called "page.php" (specified in the form's action attribute) via the query string. So, if I entered "mooseh" and pressed submit, the form will call page.php?username=mooseh. Remember that "username" was what we entered as the text input's name attribute.

Accessing a query string element in a PHP page

In PHP, all the information passed via the query string is held in the $_GET super global array (prior to PHP version 4.1.0 it was called $HTTP_GET_VARS, but that's now depreciated). To access an item, type $_GET['varName'], where varName is the name of the variable in the query string.
To demonstrate, let's create page.php to process the information from the form above:

PHP:
  1. <?
  2. if($_GET[’username’] == "")
  3. {
  4.     // no username entered
  5.     echo "You did not enter a name.";
  6. }
  7. else
  8. {
  9.     echo "Hello, " . $_GET[’username’];
  10. }
  11. ?>

So now, me typing in mooseh and pressing submit will generate the reply "Hello, mooseh".

Listing the contents of $_GET

Should you wish to find out everything that's in the query string, you can do so with the following code which displays the variables next to their values in a table:

PHP:
  1. <table border="1">
  2. <tr><th>variable</th> <th>value</th></tr>
  3. <?
  4. foreach($_GET as $variable => $value)
  5. {
  6.     echo "<tr><td>" . $variable . "</td>";
  7.     echo "<td>" . $value . "</td>";
  8. }
  9. ?>
  10. </table>

Conclusion

This article showed you how to pass information between pages with the query string, and a use which allows you to add some increased interactivity. There are a lot of other uses, for example selecting which section of a site to display based on a variable in the query string, and processing form data.

If you have a website, consider joining the Money4Banners advertising network. They will pay you £10 + £5 each month for displaying a small advert on three of your pages, regardless of traffic. American webmasters are welcome, and since £10 == $20, you make more!


8 Responses to “How to use the query string in PHP”  

  1. Gravatar Icon 1 ben

    wonderfull! thanu, u have explained me a lot.

  2. Gravatar Icon 2 zaxxon

    i followed the instruction , but other than the table i couldnt get the variables to show.

  3. Gravatar Icon 3 diorella

    good day! i am trying to know how to pass values from 1 page to another. i got it when i passed textfield but i also want to pass values from my table. isaw ur example but can u make a much clear example?
    is there a way to pass an array variable from 1 page to another?
    i hope u can help me.
    thank you in advance

  4. Gravatar Icon 4 Samuel

    is there a full tutorial on this subject?

  5. Gravatar Icon 5 Renjith

    Nice Explanation…
    How to pass more than one query string

  6. Gravatar Icon 6 Rupert

    I had to modify the code to use double quotes if($_GET[”username”] == “”)

    Then it worked for me. I am runing PHP 4.4.4

  7. Gravatar Icon 7 Vimukthi

    Thanx buddy. I understood very well, & now it is working very clearly…
    Thanxx again

  8. Gravatar Icon 8 John

    Hi,
    many thanks for this descriptions, nice to read.

    Greetz John

Leave a Reply

You must log in to post a comment.


Categories