TExcelTAD
Home › PHP › PHP Superglobals

PHP Superglobals

Superglobals are built-in arrays that PHP fills in for you before your script starts. They are available in every scope — inside functions and methods too — without the global keyword. Most of them describe the HTTP request that triggered the script: the query string, the submitted form, the cookies, the uploaded files and details about the server.

The full list

Superglobal Contains Typical use
$_GET parameters from the URL query string (?q=php&page=2) search pages, filters, pagination
$_POST fields of a form sent with method="post" logins, sign-ups, anything that changes data
$_REQUEST $_GET + $_POST (+ cookies, depending on config) merged rarely recommended — be explicit instead
$_SERVER request and server information: method, URI, headers, script name routing, detecting POST requests
$_COOKIE cookies the browser sent back remembering preferences
$_SESSION data you stored for this visitor across requests (after session_start()) logged-in user, shopping cart
$_FILES metadata about uploaded files profile pictures, attachments
$_ENV environment variables (often empty, depends on variables_order) configuration
$GLOBALS every global variable, by name reading globals from a function (read-only since 8.1)

From request to array

Suppose a browser sends this request to search.php:

http
POST /search.php?page=2 HTTP/1.1
Host: shop.example
Content-Type: application/x-www-form-urlencoded
Cookie: theme=dark

q=green+tea&sort=price

Before the first line of search.php runs, PHP has already parsed it into:

text
$_GET    = ['page' => '2']
$_POST   = ['q' => 'green tea', 'sort' => 'price']
$_COOKIE = ['theme' => 'dark']
$_SERVER['REQUEST_METHOD'] = 'POST'
$_SERVER['REQUEST_URI']    = '/search.php?page=2'
$_SERVER['HTTP_HOST']      = 'shop.example'

Two things to notice: every value arrives as a string ('2', not 2), and the URL encoding (+, %20) is already decoded.

Trying it in the playground

The playgrounds on this site run PHP from the command line, not behind a web server, so there is no real request and these arrays are mostly empty. Superglobals are ordinary arrays, though — you can fill them yourself to simulate a request and test your code:

php · try it yourself
<?php
// Simulate: GET /products.php?category=books&page=3
$_GET = ['category' => 'books', 'page' => '3'];

$category = $_GET['category'] ?? 'all';
$page = (int) ($_GET['page'] ?? 1);
$sort = $_GET['sort'] ?? 'newest';

echo "Category: $category" . PHP_EOL;
echo "Page: $page (" . gettype($page) . ')' . PHP_EOL;
echo "Sort: $sort" . PHP_EOL;
Category: books
Page: 3 (integer)
Sort: newest

Always use ?? (or isset()) when reading a superglobal: the visitor decides which parameters to send, so any key may be missing.

Checking the request method

A single script often both shows a form (GET) and processes it (POST). $_SERVER['REQUEST_METHOD'] tells the two apart:

php · try it yourself
<?php
function handle(): string
{
    if (($_SERVER['REQUEST_METHOD'] ?? 'GET') !== 'POST') {
        return 'Showing the empty form';
    }
    $name = trim($_POST['name'] ?? '');
    if ($name === '') {
        return 'Error: name is required';
    }
    return 'Thanks, ' . htmlspecialchars($name) . '!';
}

$_SERVER['REQUEST_METHOD'] = 'GET';
echo handle() . PHP_EOL;

$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST = ['name' => '   '];
echo handle() . PHP_EOL;

$_POST = ['name' => 'Tú <admin>'];
echo handle() . PHP_EOL;
Showing the empty form
Error: name is required
Thanks, Tú &lt;admin&gt;!

handle() reads $_SERVER and $_POST without any global statement — that is exactly what "super" global means. htmlspecialchars() escapes < and > so the user's input cannot inject HTML into your page.

Note: Never trust superglobals. Everything in $_GET, $_POST, $_COOKIE, $_FILES and most of $_SERVER (such as HTTP_* headers) comes from the visitor and can be faked. Validate it on the way in and escape it on the way out.

Useful $_SERVER keys

Key Example value
REQUEST_METHOD GET, POST
REQUEST_URI /search.php?page=2
QUERY_STRING page=2
SCRIPT_NAME /search.php
HTTP_HOST shop.example
HTTP_USER_AGENT the browser's identification string
REMOTE_ADDR the client's IP address
HTTPS set to a non-empty value on HTTPS requests

The exact set of keys depends on the web server and on whether PHP runs on the command line, so code should never assume a key is present.

$GLOBALS

$GLOBALS maps the name of every global variable to its value. It lets a function read a global without the global keyword:

php · try it yourself
<?php
$siteName = 'TAD Shop';
$visitors = 41;

function banner(): string
{
    return 'Welcome to ' . $GLOBALS['siteName'];
}

function countVisit(): void
{
    global $visitors; // to change a global, import it
    $visitors++;
}

echo banner() . PHP_EOL;
countVisit();
echo "Visitors: $visitors" . PHP_EOL;
Welcome to TAD Shop
Visitors: 42

Since PHP 8.1 the $GLOBALS array itself is a read-only copy: you can still write $GLOBALS['x'] = 1, but you can no longer replace the whole array. In real applications, prefer passing values as function parameters — globals make code harder to test.

Test yourself

For the URL /list.php?page=4, what is the type of $_GET['page']?

  1. int
  2. string
  3. float
  4. array

Which superglobal tells you whether the request was GET or POST?

  1. $_REQUEST['method']
  2. $_ENV['METHOD']
  3. $_SERVER['REQUEST_METHOD']
  4. $GLOBALS['method']

Why can a function use $_POST without the global keyword?

  1. Because $_POST is a constant
  2. Because superglobals are available in every scope
  3. Because functions inherit all variables
  4. It cannot