PHP CHAPTER 21

PHP Superglobals

Superglobals are predefined PHP variables available in different scopes.

📘

What is it?

Superglobals are predefined PHP variables available in different scopes.

💡

Why do we use it?

They provide access to request data, server information, sessions, cookies and uploaded files.

⚙️

How does it work?

PHP automatically populates arrays such as $_GET, $_POST and $_SERVER.

🧩

Syntax

PHP Syntax
$_GET['name'];
$_POST['email'];
$_SERVER['REQUEST_METHOD'];
💻

Example

PHP Example
<?php

echo $_SERVER['REQUEST_METHOD'];

?>
🔍

Example Explained

$_SERVER contains server and request information. REQUEST_METHOD normally displays GET or POST.
🚀

More Examples

<?php $name = $_GET['name'] ?? 'Guest'; echo htmlspecialchars($name); ?>

⚠️ Important Rules

Never trust external input. Validate and sanitize it.

❌ Common Mistakes

Using $_POST or $_GET values without checking whether they exist.

🌍 Real-World Use

Forms, authentication, file uploads and sessions.

📝 Practice Exercise

Create a page that displays selected $_SERVER values.

💻 Try It Yourself

Write and run this PHP lesson inside the AIFA Coding Lab.

Open Coding Lab →