Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
FAQs Support
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

PHP Superglobals

Superglobals are built-in variables that are always accessible regardless of scope — inside functions, classes, or files. They are prefixed with $_.

$_GET and $_POST

$_GET collects data sent via URL query strings. $_POST collects data sent via HTTP POST (form submissions). $_REQUEST contains both.

$_GET and $_POST
<?php
// URL: example.com/page.php?name=Alice&age=30
$name = $_GET['name'] ?? 'Guest';
$age  = $_GET['age']  ?? 0;
echo "Name: $name, Age: $age";

// POST data from a form
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = htmlspecialchars($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';
    echo "Username: $username";
}

// $_REQUEST combines GET, POST, and COOKIE
$search = $_REQUEST['q'] ?? '';
echo "Search: $search";
?>

$_SERVER

$_SERVER contains information about the server environment, headers, and the current request.

$_SERVER
<?php
echo $_SERVER['SERVER_NAME'];    // e.g. localhost
echo $_SERVER['HTTP_HOST'];      // e.g. www.example.com
echo $_SERVER['SCRIPT_NAME'];    // e.g. /index.php
echo $_SERVER['REQUEST_METHOD']; // GET or POST
echo $_SERVER['REMOTE_ADDR'];    // visitor's IP address
echo $_SERVER['HTTP_USER_AGENT']; // browser info
echo $_SERVER['QUERY_STRING'];   // everything after ?
echo $_SERVER['DOCUMENT_ROOT'];  // server root path
echo $_SERVER['PHP_SELF'];       // current script path
?>

$_FILES, $_ENV, $_COOKIE, $_SESSION

Other Superglobals
<?php
// $_FILES — file upload info
// <input type="file" name="photo">
$fileName = $_FILES['photo']['name']     ?? '';
$fileSize = $_FILES['photo']['size']     ?? 0;
$fileTmp  = $_FILES['photo']['tmp_name'] ?? '';
$fileErr  = $_FILES['photo']['error']    ?? 1;

if ($fileErr === UPLOAD_ERR_OK) {
    move_uploaded_file($fileTmp, "uploads/$fileName");
    echo "Uploaded: $fileName";
}

// $_ENV — environment variables
$dbHost = $_ENV['DB_HOST'] ?? 'localhost';

// $_COOKIE — read a cookie
$theme = $_COOKIE['theme'] ?? 'light';
echo "Theme: $theme";

// $_SESSION — read session data
session_start();
$_SESSION['user'] = 'Alice';
echo $_SESSION['user']; // Alice
?>

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.