5 min read
Superglobals & Handling Form Data
Prerequisites: Arrow functions
PHP automatically populates several superglobal arrays with
incoming request data — available in any scope, with no global
keyword needed.
// From an HTML form: <form method="POST"><input name="email"></form>
$email = $_POST['email'] ?? '';
// From a URL like page.php?id=5
$id = $_GET['id'] ?? null;
echo $_SERVER['REQUEST_METHOD']; // "GET" or "POST"
Common mistakes
- Reading
$_POST['email']directly without the?? ''fallback — if the field is missing, this raises a warning instead of failing gracefully. - Trusting
$_POST/$_GETvalues without validating them first — they're raw user input, covered fully in Level 3's Security Fundamentals.