Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Variables and Constants

PHP superglobals

Overview of PHP superglobals: $_GET, $_POST, $_SERVER, $_REQUEST, $_COOKIE, $_FILES, $_SESSION, $_ENV and $GLOBALS, what fills them and how to read them safely.

Superglobals are predefined variables that PHP creates before your script starts and that are visible in every scope: top-level code, functions, methods and included files. You never write global $_GET;, and you cannot declare your own superglobals. They carry the request data (query string, form fields, cookies, uploaded files), information about the server and the environment, and the session.

All superglobals are ordinary arrays, so you read them with array syntax: $_GET["page"]. Because most of them are filled from the client, treat every value as untrusted text: it may be missing, it may be an array instead of a string, and it can contain anything.

PHP
<?php
$page = $_GET["page"] ?? "home";
$agent = $_SERVER["HTTP_USER_AGENT"] ?? "unknown";

Try it

PHP Superglobals are visible inside functions

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 Which superglobals exist in a CLI script

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.

The superglobals

Variable Filled from Reference
$_GET The URL query string (?a=1&b=2) $_GET
$_POST Form bodies sent as application/x-www-form-urlencoded or multipart/form-data $_POST
$_REQUEST $_GET, $_POST and $_COOKIE combined $_REQUEST
$_SERVER The web server and PHP: headers, paths, method, script name $_SERVER
$_COOKIE The Cookie request header $_COOKIE
$_FILES Uploaded files $_FILES
$_SESSION Data stored on the server for the current visitor $_SESSION
$_ENV Environment variables (depending on variables_order) $_ENV and getenv
$GLOBALS Every variable in the global scope $GLOBALS

Usage notes

  • Always global scope. Inside a function $_GET works without global. That is convenient and also a hidden dependency: a function that reads $_POST directly is hard to test. Pass the values in as parameters instead.
  • Not always defined. In the command line there is no request, so $_GET, $_POST, $_COOKIE, $_FILES and $_REQUEST exist but are empty, and $_SESSION does not exist until session_start() runs.
  • Writable, but only inside your script. You can assign to $_GET["x"] or replace the whole array. This is useful in tests and demos like the ones on this page, but it never changes the real request.
  • Untrusted input. Query strings, form fields, cookies and most $_SERVER["HTTP_*"] entries come from the client. Validate with filter_var() or filter_input() and escape output with htmlspecialchars().
  • Values can be arrays. ?tag[]=a&tag[]=b makes $_GET["tag"] an array. Code that assumes a string must check the type.
  • Reading is quiet only with ??. $_GET["id"] on a missing key raises a warning (Undefined array key "id"). Use ?? or isset().
  • Variable variables and superglobals. Superglobals cannot be used as variable variables inside functions and methods (${"_GET"} does not work there). Use $GLOBALS instead. For the same reason, $GLOBALS["_REQUEST"] and $GLOBALS["_ENV"] are only filled when the script also mentions those names literally.
  • Old behavior removed. register_globals, which turned request fields into plain variables, was removed in PHP 5.4. The $HTTP_GET_VARS family went away in PHP 5.4 as well.