Superglobal variables are built-in variables in the form of arrays that are always available regardless of scope. This means they are accessible in all parts of the script: in functions, methods, and classes, without having to use the global keyword.
Superglobal variables are built-in arrays in PHP that are
automatically accessible anywhere in the code – regardless of whether you are in a regular script,
inside a function, or inside class methods. Unlike regular global variables, you do not need to write the
global $variable; command for superglobals, as they are always ready to use and handle receiving
data from the browser, session, or server.
| Superglobal Variable | Description | Usage Example |
|---|---|---|
$_COOKIE |
HTTP Cookies stored in the user's browser. | echo isset($_COOKIE["user"]) ? $_COOKIE["user"] : "Guest"; |
$_ENV |
Information about the server environment variables. | echo "Current environment: " . ($_ENV["APP_ENV"] ?? "Not set"); |
$_FILES |
Data regarding items uploaded via HTTP POST forms. | echo "Uploaded file name: " . ($_FILES["image"]["name"] ?? "No file"); |
$_GET |
Parameters passed in the URL string (after the question mark). | echo "Selected ID on page: " . htmlspecialchars($_GET["id"] ?? "not selected"); |
$GLOBALS |
An array containing all global variables within the script. | $name = "John"; function test() { echo "Author: " . $GLOBALS["name"]; } test(); |
$_POST |
Data submitted via an HTTP POST form. | echo "Logged-in user: " . htmlspecialchars($_POST["username"] ?? ""); |
$_REQUEST |
Combines the contents of $_GET, $_POST, and $_COOKIE. | echo "Search query (GET or POST): " . htmlspecialchars($_REQUEST["search"] ?? ""); |
$_SERVER |
Data about the server and the execution environment. | echo "Visitor IP address: " . $_SERVER["REMOTE_ADDR"]; |
$_SESSION |
Data regarding sessions that persist across the entire site visit. | session_start(); echo "Welcome back, " . ($_SESSION["user_id"] ?? "Guest"); |