PHP
🌐 Super Globals in PHP
Super Globals are built-in predefined associative arrays in PHP that are always accessible in all scopes throughout a script. You don't need to define them or declare global $variable to access them inside functions, methods, or files.
- Basic Concept: Super globals automatically capture data from user requests, forms, browser headers, cookies, active sessions, and server environments.
⚡ Most Commonly Used Super Global Variables
1️⃣ $GLOBALS
A superglobal array used to access global variables from anywhere in the PHP script (even inside functions or methods).
$a = 10;
function test() {
// Accessing global variable $a inside local scope
echo $GLOBALS['a'];
}
test(); // Outputs: 10
2️⃣ $_SERVER
Holds information about headers, paths, script locations, and server execution environments.
echo $_SERVER['SERVER_NAME']; // Host name of the server
echo $_SERVER['REQUEST_METHOD']; // GET, POST, etc.
echo $_SERVER['SCRIPT_NAME']; // Path of current script
3️⃣ $_GET
Collects data sent via URL parameters (query string). Visible in the address bar and used for non-sensitive data transfers.
// Example URL: site.com/index.php?isim=Ali
if (isset($_GET['isim'])) {
echo "Hello, " . $_GET['isim']; // Outputs: Hello, Ali
}
4️⃣ $_POST
Collects data sent via HTTP POST method (typically HTML form submissions). Values are hidden in the request body, making it ideal for passwords and sensitive inputs.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = $_POST['email'];
echo "Submitted email: " . $email;
}
5️⃣ $_REQUEST
Combines the contents of $_GET, $_POST, and $_COOKIE into a single array.
-
Best Practice: Explicitly use
$_GETor$_POSTinstead of$_REQUESTfor clarity and security.
$name = $_REQUEST['isim'];
6️⃣ $_FILES
An associative array containing items uploaded via HTTP POST multipart form requests (file name, MIME type, size, temporary path, and error status).
$fileName = $_FILES['resim']['name'];
$fileTmp = $_FILES['resim']['tmp_name'];
$fileSize = $_FILES['resim']['size'];
7️⃣ $_COOKIE
Retrieves values stored on the client's browser using HTTP Cookies.
if (isset($_COOKIE['kullanici'])) {
echo "Welcome back, " . $_COOKIE['kullanici'];
}
8️⃣ $_SESSION
Stores data across multiple pages for an individual user on the server side. Requires session_start() prior to output.
session_start();
$_SESSION['user'] = "Ali";
echo "Active Session User: " . $_SESSION['user'];
📊 Quick Summary Table
Here is a reference summary for PHP superglobals:
| Variable | Purpose & Description |
|---|---|
$GLOBALS |
Access global variables from anywhere in script scope. |
$_SERVER |
Server environment, headers, and execution information. |
$_GET |
Collects parameters passed in the URL. |
$_POST |
Collects hidden form payloads via HTTP POST. |
$_REQUEST |
Combined array of GET, POST, and COOKIE data. |
$_FILES |
Contains uploaded file payloads and metadata. |
$_COOKIE |
Retrieves persistent client-side browser cookies. |
$_SESSION |
Manages stateful user session data on the server. |
-
📝 Summary Takeaways
-
Superglobals are case-sensitive and always start with an underscore except for
$GLOBALS. -
Always sanitize and validate inputs from
$_GET,$_POST, and$_COOKIEbefore processing.
