PHP Superglobals

$_GET, $_POST, $_SERVER and friends.

✍️ 2 exercises in this lesson

Superglobals are built-in arrays available everywhere in your script:

  • $_GET: values from the URL query string (?name=Ali).
  • $_POST: values from a submitted form (method="post").
  • $_REQUEST: GET + POST + COOKIE.
  • $_SERVER: server and request info, e.g. $_SERVER["REQUEST_METHOD"].
  • $_SESSION, $_COOKIE, $_FILES, $_ENV, $GLOBALS
Never trust superglobal input. Validate it, and escape it with htmlspecialchars() before printing.

Key Takeaways: PHP Superglobals

Mastering PHP Superglobals helps you write clean and reliable PHP code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

PHP Superglobals Code Example

PHP
<?php
// URL: page.php?name=Ali&city=Lahore
$name = htmlspecialchars($_GET["name"] ?? "Guest");
$city = htmlspecialchars($_GET["city"] ?? "unknown");

echo "Hello $name from $city<br>";
echo "Method: " . $_SERVER["REQUEST_METHOD"];
Output
Hello Ali from Lahore
Method: GET
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

PHP Superglobals Practice Exercises

Check what you learned in “PHP Superglobals”.

/ 2 solved
Fill in the blank

Read the "q" value from the URL query string.

$q = ["q"];
✓ Correct! Not quite. Try again.
Multiple choice

Which superglobal holds data from a form sent with method="post"?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services