In PHP, superglobals are built-in variables that are always accessible, regardless of scope—meaning you can access them inside functions, classes, or even without explicitly declaring them as global. They are automatically available in all parts of your script.
Superglobals are mainly used to access data from forms, sessions, server information, cookies, etc.
Here’s a list of some commonly used superglobals:
| Superglobal | Purpose |
|---|---|
$_GET | Retrieves data sent via the URL (query string) using the GET method. |
$_POST | Retrieves data sent via a form using the POST method. |
$_REQUEST | Retrieves data sent via GET, POST, or COOKIE. |
$_SESSION | Stores and retrieves session variables. |
$_COOKIE | Stores and retrieves cookie data. |
$_SERVER | Contains server and execution environment information (like headers, paths, and script locations). |
$_FILES | Retrieves information about uploaded files via forms. |
$_ENV | Retrieves environment variables. |
$_GLOBALS | References all global variables available in the script. |
✅ Example usage:
// Using $_GET
echo "Hello, " . $_GET['name'];
// Using $_POST
echo "Your email is " . $_POST['email'];
// Using $_SESSION
session_start();
$_SESSION['user'] = 'Himanshu';
echo $_SESSION['user'];
// Using $_SERVER
echo $_SERVER['HTTP_USER_AGENT'];
Superglobals are always available, but you need to start a session (session_start()) before using $_SESSION.
