PHP Global Variables - Superglobals
In PHP, superglobals are built-in global arrays that are always accessible, regardless of scope — you can access them from any function, class, or file without needing to use global
or other special mechanisms.
Here are the main PHP superglobals:
1. $_GLOBALS
-
An associative array that contains all global variables.
-
Useful for accessing global variables from within functions.
2. $_SERVER
-
Contains information about headers, paths, and script locations.
-
Common entries:
-
$_SERVER['PHP_SELF']
: The filename of the currently executing script. -
$_SERVER['SERVER_NAME']
-
$_SERVER['REQUEST_METHOD']
-
3. $_REQUEST
-
Collects data after submitting an HTML form with either GET or POST.
-
Also includes COOKIE data.
4. $_POST
-
Collects data submitted via HTTP POST method.
-
Secure for handling sensitive data (e.g., passwords).
5. $_GET
-
Collects data sent in the URL via HTTP GET.
-
Less secure; used for non-sensitive data like search queries.
6. $_FILES
-
Handles file uploads.
7. $_ENV
-
Contains environment variables passed to the script.
8. $_COOKIE
-
Contains cookies sent to the script.
9. $_SESSION
-
Used to store session variables across multiple pages.