What are Super Global Variables in PHP ? See Example

Super global variables in PHP are built-in variables that are available in all scopes—inside functions, classes, and files—without requiring global keyword. They allow PHP scripts to access server data, form data, session data, cookies, and environment variables easily.

These variables play a major role in handling forms, sessions, server requests, and more.


List of Super Global Variables in PHP

PHP provides nine superglobal arrays:

  1. $GLOBALS
  2. $_SERVER
  3. $_REQUEST
  4. $_POST
  5. $_GET
  6. $_FILES
  7. $_ENV
  8. $_COOKIE
  9. $_SESSION

Let’s understand each.


1. $GLOBALS

Used to access global variables from anywhere.

$x = 10;

function test() {
    echo $GLOBALS['x']; // 10
}
test();

2. $_SERVER

Contains information about the server, headers, and script paths.

echo $_SERVER['PHP_SELF'];   // current file name
echo $_SERVER['SERVER_NAME']; // server name
echo $_SERVER['REQUEST_METHOD']; // GET or POST

3. $_REQUEST

Contains data from both GET and POST requests (and cookies).

$name = $_REQUEST['username'];

⚠ Not recommended for sensitive data.


4. $_POST

Collects form data sent using POST method.

echo $_POST['email'];

5. $_GET

Collects data sent in URL query.

echo $_GET['id']; // from URL ?id=10

6. $_FILES

Used for handling file uploads.

print_r($_FILES['photo']);

7. $_ENV

Stores environment variables.

echo $_ENV['PATH'];

8. $_COOKIE

Stores and retrieves cookie values.

echo $_COOKIE['user'];

9. $_SESSION

Used to store session data across multiple pages.

session_start();
echo $_SESSION['username'];

Why Superglobals Are Important?

✔ Available everywhere without declaring
✔ Essential for form handling
✔ Manage sessions, cookies, and file uploads
✔ Provide server and environment information
✔ Improve communication between PHP and browser


Conclusion

Super global variables form the backbone of PHP programming.
Whether you’re collecting form data, uploading files, tracking users with sessions, or accessing server information—superglobals make it all possible.


Citations

🔗 View other articles about PHP:
http://savanka.com/category/learn/php/

🔗 External PHP Documentation:
https://www.php.net/manual/en/

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *