PHP Superglobal Variables

Introduction

In PHP, variables play a crucial role in storing and transferring data within an application. Normally, variables have a limited scope, meaning they can only be accessed within the function or block where they are defined. However, PHP provides a special type of variable called Superglobal Variables that can be accessed from anywhere in the script.

Superglobals make it easier to handle user input, server information, sessions, and cookies without worrying about variable scope. They are widely used in web development, especially when working with forms, APIs, and dynamic content.

What is a PHP Superglobal Variable?

A Superglobal Variable in PHP is a predefined variable that is always accessible, regardless of scope. You can use superglobal variables inside functions, classes, or anywhere in your script without needing to declare them as global.

Note: Superglobals are automatically available in all scopes.

📖
Important Points:
  • Predefined by PHP
  • Accessible from anywhere
  • Used to collect data from users, server, and environment

List of PHP Superglobal Variables

Here are the most commonly used PHP superglobal variables:

  • $_GET
  • $_POST
  • $_REQUEST
  • $_SERVER
  • $_SESSION
  • $_COOKIE
  • $_FILES
  • $_ENV
  • $GLOBALS

Syntax

Superglobal variables are used like normal arrays:


$_SUPERGLOBAL['key'];

Example:


echo $_GET['name'];

Examples:

1. $_GET Example

Used to collect data from URL parameters.


<?php
print_r($_GET);
?>

If the URL is:


example.php?name=John&age=25

Output:


Array ( [name] => John [age] => 25 )

2. $_POST Example

Used to collect form data sent via the POST method.


<?php
print_r($_POST);
?>

3. $_SERVER Example

Provides server-related information.


<?php
print_r($_SERVER);
?>

Common values:

  • HTTP_HOST
  • REQUEST_METHOD
  • SERVER_NAME

4. $_ENV Example

Used to access environment variables.


<?php
print_r($_ENV);
?>

5. $_REQUEST Example

Combines $_GET, $_POST, and $_COOKIE.


<?php
print_r($_REQUEST);
?>

6. $_SESSION Example

Stores user data across multiple pages.


<?php
session_start();
$_SESSION['user'] = "John";
echo $_SESSION['user'];
?>

7. $_COOKIE Example

Used to store small pieces of data in the user’s browser.


<?php
setcookie("user", "John", time() + 3600);
echo $_COOKIE['user'];
?>

8. $_FILES Example

Used for file uploads.


<?php
print_r($_FILES);
?>

9. $GLOBALS Example

Used to access global variables inside functions.


<?php
$x = 10;
function test() {
   echo $GLOBALS['x'];
}
test();
?>

Real-Life Example

Scenario 1: Form Handling


<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {
  
   $username = $_POST['username'];
  
   if(!empty($username)) {
       echo "Welcome " . $username;
   } else {
       echo "Please enter your name";
   }
}
?>

Why this is useful:

  • Handles user input
  • Validates data
  • Creates dynamic responses

Scenario 2: Display User IP Address


<?php
echo "Your IP Address is: " . $_SERVER['REMOTE_ADDR'];
?>

Why it is Used

Superglobal variables are essential in PHP for several reasons:

1. Handling User Input: They help collect data from forms, URLs, and APIs.

2. Accessing Server Information: You can retrieve server-related data like hostname, request method, and IP address.

3. Managing Sessions and Cookies: Superglobals help store user data across pages.

4. Simplifying Code: No need to pass variables between functions manually.

5. Dynamic Web Development: They enable dynamic interaction between client and server.

Common Mistakes

1. What are Superglobal variables in PHP?


echo $_GET['name'];

This can cause errors or security issues.

2. Name some PHP superglobals.


echo $_POST['username'];

Note: If not set, it throws a warning.

Correct:


if(isset($_POST['username'])) {
   echo $_POST['username'];
}

3. Ignoring Security Risks

Using superglobals without sanitization can lead to:

  • SQL Injection
  • XSS attacks

4. Confusing $_REQUEST

It mixes multiple sources, which can cause unexpected results.

5. What is $_REQUEST?


$_SESSION['user'] = "John";

Note: Will not work without session_start().

Conclusion

PHP Superglobal variables are powerful tools that simplify web development by providing easy access to important data like user input, server details, and session information. They eliminate the need for complex variable passing and make code more efficient.

However, while using superglobals, developers must be careful about security and validation to prevent vulnerabilities.

Related PHP Tutorials