Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Variables and Constants

PHP $_GET superglobal

Reference for PHP $_GET: read URL query string parameters, handle missing keys and array parameters, and validate the input before you use it.

$_GET is an associative array holding the parameters from the URL query string, the part after the ?. For /products?category=books&page=2, PHP fills $_GET with ["category" => "books", "page" => "2"]. Despite the name, it is filled for every request method, not only GET: a POST request to a URL with a query string still has a populated $_GET.

All values are strings (or arrays of strings), no matter how they look. "2" is not the integer 2. Because anyone can type anything into a URL, every entry must be checked before use.

PHP
<?php
$category = $_GET["category"] ?? "all";
$page = filter_var($_GET["page"] ?? 1, FILTER_VALIDATE_INT) ?: 1;

Try it

In the command line no request exists, so these examples build the same array PHP would build from a query string with parse_str().

PHP From query string to $_GET

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Arrays, nesting and safe reading

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
$_GET
$_GET["name"]
$_GET["list"][]
Part Description
$_GET Superglobal array, empty (never null) when the URL has no query string.
Keys The parameter names, always strings or integers that look like numbers (?5=x gives key 5).
Values Strings, or nested arrays for names such as a[]=1 or a[b]=1.

Type: array<string|int, string|array>

Usage notes

  • Everything is a string. ?page=2 gives "2". Convert with (int) only after checking, or use filter_var() with FILTER_VALIDATE_INT.
  • Missing keys warn. $_GET["id"] on a missing key raises Warning: Undefined array key "id". Use ?? or isset().
  • ?flag and ?flag= both give an empty string. The key exists, so isset() is true and empty() is also true. Use array_key_exists() or compare with "".
  • Dots and spaces in names. PHP rewrites them: ?a.b=1 becomes the key a_b. Avoid dots in parameter names.
  • Duplicate names. The last value wins: ?x=1&x=2 gives "2". Use x[]=1&x[]=2 for lists.
  • Array parameters change the type. ?id[]=1 makes $_GET["id"] an array, and passing it to a string function throws a TypeError. Check with is_string().
  • Never use $_GET for changes. Links can be prefetched, cached and followed by crawlers. Actions that change data should use POST (see $_POST) and a CSRF token.
  • Do not put secrets in the URL. Query strings end up in logs, browser history and Referer headers.
  • Escape on output, bind on queries. Print with htmlspecialchars() and pass values to the database through prepared statements, never by concatenation.
  • Size limits. The web server limits URL length (often 4-8 KB), and max_input_vars (default 1000) limits how many variables PHP parses.
  • Building the other direction. http_build_query() creates a query string from an array.