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 $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().
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.
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
$_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=2gives"2". Convert with(int)only after checking, or usefilter_var()withFILTER_VALIDATE_INT. - Missing keys warn.
$_GET["id"]on a missing key raisesWarning: Undefined array key "id". Use??orisset(). ?flagand?flag=both give an empty string. The key exists, soisset()is true andempty()is also true. Usearray_key_exists()or compare with"".- Dots and spaces in names. PHP rewrites them:
?a.b=1becomes the keya_b. Avoid dots in parameter names. - Duplicate names. The last value wins:
?x=1&x=2gives"2". Usex[]=1&x[]=2for lists. - Array parameters change the type.
?id[]=1makes$_GET["id"]an array, and passing it to a string function throws aTypeError. Check withis_string(). - Never use
$_GETfor changes. Links can be prefetched, cached and followed by crawlers. Actions that change data should usePOST(see$_POST) and a CSRF token. - Do not put secrets in the URL. Query strings end up in logs, browser history and
Refererheaders. - 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.
Related
$_POST,$_REQUEST,$_SERVER,filter_input(),http_build_query()- Lessons: Forms, Form validation, Superglobals