Skip to content

Web Nerdism w/ PHP 101 › Basics

Query Strings

Phillip Harrington Nov 2026

Anatomy of a URL named the part after the ?. It's the simplest way to send a little data with a request: a search, a sort order, a page number, a filter. It's also the most public. It sits in the address bar, where anyone can read it, copy it, bookmark it, or change it.

What's a Query String?

/search?q=red+shoes&page=2&tag[]=sale&tag[]=new
The browser at localhost:8000/?q=red+shoes&page=2&tag[]=sale&tag[]=new, showing Searching for: red shoes, Page: 2, Tags: sale, new, and the $_GET array.
The plus became a space, and tag became an array.

Everything after the ?:

  • Pairs of name=value, joined with &.
  • Encoded, so they survive the trip: a space is + or %20, an & inside a value is %26.
  • Repeatable. PHP's convention: a name ending in [], like tag[], collects every value into an array. filter[color]=red makes an array with keys.

How It Works

  1. Something builds the URL: a link, a search form, JavaScript, or a person typing.
  2. The browser sends it as part of the request line: GET /search?q=red+shoes.
  3. PHP splits it up, decodes it, builds any arrays, and hands it to you as $_GET.
The query string, split at each &What PHP hands you in $_GETq=red+shoes[q] => red shoespage=2[page] => 2tag[]=sale[tag] => [0] => saletag[]=new[1] => new+ becomes a space[] collects into an array
One string in, one array out. PHP does the splitting and decoding before your code runs.

Use a query string for things that describe what to show: what to search for, how to sort, which page. Those are safe to repeat, worth bookmarking, and worth sharing. Don't use one for anything that changes data (that's a POST, from Request and Response), or for anything secret.

And notice step one: "a person typing." As I've said before, never trust input. Whatever you expect in $_GET, someone can send something else. A missing value, an empty one, a word where you wanted a number, a negative page number, an array where you wanted a string. Your code decides what to accept.

Try It

A pretend search page that trusts nothing. In a folder, make index.php:

<?php
header('Content-Type: text/plain');

// Never trust input: each one can be missing, the wrong type, or nonsense.
$q = is_string($_GET['q'] ?? null) ? $_GET['q'] : '';
$page = filter_var($_GET['page'] ?? 1, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]) ?: 1;
$tags = array_filter((array) ($_GET['tag'] ?? []), 'is_string');

echo "Searching for: $q\n";
echo "Page: $page\n";
echo 'Tags: ', implode(', ', $tags), "\n\n";
echo "The contents of \$_GET:\n";
print_r($_GET);

query-strings/index.php on GitHub

Start the server the usual way:

docker run --rm -it -p 8000:8000 -v "$PWD":/app -w /app php:8.4-cli php -S 0.0.0.0:8000

The usual server: PHP's built-in one, in a container, on port 8000.

Open this, and see what PHP made of it:

http://localhost:8000/?q=red+shoes&page=2&tag[]=sale&tag[]=new

The + became a space, and tag became an array. Now send it garbage, the way someone eventually will, with a bot, a hundred times a minute:

http://localhost:8000/?q[]=x&page=banana&tag=solo
The browser at localhost:8000/?q[]=x&amp;page=banana&amp;tag=solo, showing an empty search, Page: 1, Tags: solo, and $_GET holding an array for q and the word banana for page.
Garbage in, page 1 out.

Look at the raw $_GET at the bottom: q arrived as an array and page as a word. The code at the top shrugged both off: an empty search, page 1. Try page=-5 too, or try to make the Page line say anything but a positive whole number. The PHP script won't let you. And try a search for <script>alert(1)</script>: it comes back as plain text, because the page says it's text/plain. On an HTML page you'd have to escape it yourself. That's the XSS lesson, in Security.

New to Docker or the terminal? Tools has the setup.

In PHP

  • $_GET is the query string, decoded, with arrays already built. Every value in it is input. Check it.
  • filter_var() checks and converts in one step: FILTER_VALIDATE_INT with a min_range is a page number, FILTER_VALIDATE_EMAIL an email address. It returns false when the value doesn't qualify.
  • http_build_query() builds a query string from an array, encoding everything: ['q' => 'red shoes', 'page' => 2] becomes q=red+shoes&page=2. Use it for every link you build; don't glue strings together.

Gotchas

  • A string can arrive as an array. Add [] to any name, and PHP makes it an array. Code that passes $_GET['q'] straight to a function expecting a string then crashes. Check the type before you use it.
  • Query strings are public. They're written to server logs, saved in browser history, and can be passed to the next site you visit. Never put a password or a secret in one. (A private window keeps your own searches out of your history, but the site's server logs still get them.)
  • A GET mustn't change anything. Browsers prefetch links, email scanners follow them, and search engines crawl them, so a link like ?delete=42 will get followed by something that never meant to delete anything. That's the server's job to prevent: change things only on a POST, and refuse to on a GET.
  • They have a length limit. Around 2,000 characters is safe everywhere. Big data goes in a POST body instead.

Put the page's state in the URL

If a page shows a search, a filter, a sort order or a page number, keep it in the query string. Then the Back button works, a reload shows the same thing, and a bookmark or a pasted link brings someone exactly where you were.

You built these by hand to see how they work. Forms can build them for you, and can send their data in a POST body instead. Next: forms.