PHP Global Variables – Superglobals

In PHP, global variables are variables that can be accessed from any part of the PHP script, including inside functions, methods, and files. PHP provides several special variables called “superglobals” that are automatically available in all scopes.

These superglobals are pre-defined and contain useful information that can be accessed and manipulated throughout your PHP code. Here are some commonly used PHP superglobals:


Suppose you have a URL with parameters like You can access the values using $_GET:

$name = $_GET['name'];
$age = $_GET['age'];

echo "Name: $name";  // Output: Name: John
echo "Age: $age";    // Output: Age: 25


Let’s say you have a form with input fields named “username” and “password“. You can access the form data using $_POST:

$username = $_POST['username'];
$password = $_POST['password'];

echo "Username: $username";
echo "Password: $password";


If you have set a cookie named “language” with the value “en“, you can access it using $_COOKIE:

$language = $_COOKIE['language'];

echo "Language: $language";  // Output: Language: en


Suppose you have stored the user’s name in a session variable named “user“:

session_start();  // Start the session
$_SESSION['user'] = "John";  // Set the session variable
$user = $_SESSION['user'];
echo "Welcome, $user";  // Output: Welcome, John


You can access various server-related information using $_SERVER:

$ip = $_SERVER['REMOTE_ADDR'];  // Get the user's IP address
$method = $_SERVER['REQUEST_METHOD'];  // Get the request method

echo "IP Address: $ip";
echo "Request Method: $method";


When a file is uploaded via a form field named “fileUpload“, you can access the uploaded file’s details using $_FILES:

$fileName = $_FILES['fileUpload']['name'];
$fileSize = $_FILES['fileUpload']['size'];
$fileType = $_FILES['fileUpload']['type'];

echo "File Name: $fileName";
echo "File Size: $fileSize";
echo "File Type: $fileType";


If you have set an environment variable named “DB_HOST“, you can access it using $_ENV:

$dbHost = $_ENV['DB_HOST'];

echo "Database Host: $dbHost";

These examples demonstrate how to access and utilize the values stored in PHP superglobals. Remember to validate and sanitize the data before using it to ensure security and prevent potential vulnerabilities.