PHP · Chapter 23 of 44

PHP Superglobals

Superglobals are built-in PHP variables that are always accessible in every scope — inside functions, classes, and files — without needing to use the global keyword. They provide access to form data, server information, cookies and more.

Common superglobals include $_GET, $_POST, $_SERVER, $_SESSION, $_COOKIE, $_FILES, and $_REQUEST, each holding a different category of data related to the current request.

Syntax
$_GET["key"];
$_POST["key"];
$_SERVER["key"];

Common superglobals

$_GET and $_POST hold form data sent via GET or POST requests. $_SERVER holds information about headers, paths and script locations. $_SESSION and $_COOKIE store persistent data between requests.

Why superglobals matter

Because superglobals are automatically available everywhere, you never need to pass them explicitly into functions to access request or server data.

Example 1 (php)
<?php
  echo $_SERVER["PHP_SELF"];
?>
Output
/index.php

$_SERVER["PHP_SELF"] returns the path of the currently executing script.

Example 2 (php)
<?php
  // Assume URL is page.php?name=Amy
  echo $_GET["name"];
?>
Output
Amy

$_GET reads query string parameters passed in the URL.

Key points

  • Superglobals are available in every scope without special declaration.
  • $_GET and $_POST hold submitted form data.
  • $_SERVER holds request and server environment information.
  • $_SESSION and $_COOKIE store data across multiple requests.
💡 Note: Always validate and sanitize superglobal data before using it, since it comes directly from user input.

📝 Quick Quiz

1. Which superglobal holds data from a GET request?

2. Which superglobal provides server and request information?

3. Do you need the global keyword to access superglobals in a function?