PHP superglobals are predefined variables accessible throughout a PHP script. They provide access to request data, server information, cookies, sessions, uploaded files, and environment variables.
- PHP provides nine superglobals: $GLOBALS, $_SERVER, $_REQUEST, $_GET, $_POST, $_FILES, $_ENV, $_COOKIE, and $_SESSION.
- Most superglobals are associative arrays containing key-value pairs.
- Their contents depend on the request, PHP configuration, and execution environment.
$GLOBALS
$GLOBALS provides access to global variables from any scope, including inside functions.
<?php
$name = "Jenny";
function displayName() {
echo $GLOBALS['name'];
}
displayName();
?>
$_GET
$_GET collects data sent through URL query parameters. It is commonly used for search queries, filters, and pagination. Avoid transmitting sensitive information through URLs.
<?php
$name = $_GET['name'] ?? 'Guest';
echo htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
?>
For a URL such as example.com?name=Jenny, the output is John.
$_POST
$_POST collects form data sent through HTTP POST requests, typically using application/x-www-form-urlencoded or multipart/form-data.
- Commonly used for registration forms and other form submissions.
- Use HTTPS to protect data during transmission; POST does not encrypt data.
<?php
$name = $_POST['name'] ?? 'Guest';
echo htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
?>
If the submitted form contains name=Jenny, the output is John.
$_REQUEST
$_REQUEST can contain data from GET, POST, and COOKIE requests, depending on PHP configuration. Use $_GET or $_POST directly when the data source matters.
<?php
$name = $_REQUEST['name'] ?? 'Guest';
echo htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
?>
The output is Jenny if the configured request sources contain name=Jenny.
$_SERVER
$_SERVER contains information about the server environment, current script, and HTTP request. Common entries include REQUEST_METHOD, SERVER_NAME, and HTTP_HOST.
<?php
echo $_SERVER['REQUEST_METHOD'] ?? 'Unknown';
?>
Output for GET request:
GET$_FILES
$_FILES contains information about files uploaded through an HTTP POST request using a form with enctype="multipart/form-data".
<?php
if (
isset($_FILES['file']) &&
$_FILES['file']['error'] === UPLOAD_ERR_OK
) {
$destination = 'uploads/' . basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $destination)) {
echo "File uploaded successfully.";
} else {
echo "File upload failed.";
}
}
?>
The uploads/ directory must exist and be writable. In production, validate file types and sizes, and use generated filenames instead of trusting the original filename.
$_ENV
$_ENV provides access to environment variables available to the PHP process. These variables can store application configuration, but their availability depends on the environment and PHP configuration.
<?php
$environment = $_ENV['APP_ENV'] ?? 'development';
echo $environment;
?>
$_COOKIE
$_COOKIE accesses cookies sent by the browser with an HTTP request. Cookies commonly store user preferences or session identifiers.
<?php
setcookie('username', 'Jenny', [
'expires' => time() + 3600,
'path' => '/',
'secure' => true,
'httponly' => true,
'samesite' => 'Lax'
]);
echo "Cookie set.";
?>
The cookie expires in one hour. The secure option requires HTTPS. A newly set cookie is normally available in $_COOKIE on the next request.
$_SESSION
$_SESSION stores user-specific data across requests. Call session_start() before accessing session data and before sending output.
<?php
session_start();
$_SESSION['username'] = 'Jenny';
echo $_SESSION['username'];
?>
Session data is generally stored on the server and associated with a session identifier maintained by the browser.

