0Pricing
PHP Academy · Lesson

Reading Form Data with Superglobals

Access form fields via _GET and _POST arrays.

Reading Form Data with Superglobals is a free PHP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

PHP Superglobals

Superglobals are built-in PHP arrays available in all scopes — you don't need global to access them in functions.

Form-related superglobals: $_GET, $_POST, $_FILES, $_REQUEST

Accessing $_POST Fields

Read POST form values using array key access with a default fallback:

<?php
// Safe access with null coalescing
$username = $_POST['username'] ?? '';
$email    = $_POST['email']    ?? '';
$age      = (int) ($_POST['age'] ?? 0);

var_dump($username, $email, $age);

Accessing $_GET Parameters

Read URL query string parameters from $_GET:

<?php
// URL: /products.php?category=electronics&page=2&sort=price

$category = $_GET['category'] ?? 'all';
$page     = max(1, (int) ($_GET['page'] ?? 1));
$sort     = in_array($_GET['sort'] ?? '', ['price', 'name', 'date'])
            ? $_GET['sort']
            : 'name';

echo "Category: $category, Page: $page, Sort: $sort";

Handling Checkboxes

Unchecked checkboxes are NOT submitted — always check with isset or ??:

<?php
// HTML: <input type="checkbox" name="newsletter">

$wantsNewsletter = isset($_POST['newsletter']);
// or:
$wantsNewsletter = ($_POST['newsletter'] ?? null) !== null;

echo $wantsNewsletter ? 'Subscribed' : 'Not subscribed';

Multi-Select and Checkboxes Arrays

Use array-style name attributes to collect multiple values:

<!-- HTML -->
<!-- <input type="checkbox" name="tags[]" value="php"> -->
<!-- <input type="checkbox" name="tags[]" value="mysql"> -->

<?php
$tags = $_POST['tags'] ?? [];

if (!is_array($tags)) $tags = [];

foreach ($tags as $tag) {
    echo htmlspecialchars($tag) . PHP_EOL;
}

$_SERVER Information

$_SERVER provides server and request environment data:

<?php
echo $_SERVER['REQUEST_METHOD'];   // GET or POST
echo $_SERVER['REQUEST_URI'];       // /page.php?foo=bar
echo $_SERVER['HTTP_HOST'];         // example.com
echo $_SERVER['REMOTE_ADDR'];       // client IP
echo $_SERVER['HTTP_USER_AGENT'];   // browser string

filter_input()

filter_input() reads and sanitizes superglobal values in one step:

<?php
// Retrieve and validate an integer from POST
$age = filter_input(INPUT_POST, 'age', FILTER_VALIDATE_INT);
if ($age === false) {
    echo 'Invalid age';
}

// Retrieve and sanitize a string from GET
$name = filter_input(INPUT_GET, 'name', FILTER_SANITIZE_SPECIAL_CHARS);
echo $name;

filter_var() for Validation

Validate and sanitize values already in variables:

<?php
$email = 'user@example.com';
$ip    = '192.168.1.1';
$url   = 'https://example.com';

var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));  // string
var_dump(filter_var($ip,    FILTER_VALIDATE_IP));     // string
var_dump(filter_var($url,   FILTER_VALIDATE_URL));    // string

FILTER_SANITIZE vs FILTER_VALIDATE

Two modes of filter_var:

  • FILTER_VALIDATE_* — returns the value if valid, false if not
  • FILTER_SANITIZE_* — strips/encodes dangerous characters and returns clean value

Handling $_FILES

$_FILES contains uploaded file metadata — each file has five keys:

<?php
if (isset($_FILES['avatar'])) {
    $file = $_FILES['avatar'];
    
    echo $file['name'];     // original filename: photo.jpg
    echo $file['type'];     // MIME type: image/jpeg
    echo $file['size'];     // bytes: 45231
    echo $file['tmp_name']; // temp path on server
    echo $file['error'];    // 0 = no error
}

Whole-Form Processing Pattern

A clean pattern for handling form submission with validation:

<?php
$errors = [];
$name   = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['name'] ?? '');
    if (empty($name)) {
        $errors[] = 'Name is required';
    }
    if (empty($errors)) {
        // process...
        header('Location: /thanks.php');
        exit;
    }
}

Quick Check

What does an unchecked checkbox send to the PHP server?

Recap: Superglobals

Key superglobals for forms:

  • $_GET — URL parameters
  • $_POST — request body
  • $_FILES — uploaded file info
  • $_SERVER — server/request environment
  • Use ?? for safe default access
  • Use filter_input() for combined retrieval and validation

Frequently asked questions

Is the “Reading Form Data with Superglobals” lesson free?

Yes — the full text of “Reading Form Data with Superglobals” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Reading Form Data with Superglobals”?

Access form fields via _GET and _POST arrays. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Reading Form Data with Superglobals” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. GET vs POST: When to Use Each
  2. Reading Form Data with Superglobals
  3. Input Validation Techniques
  4. Sanitizing User Input
← Back to PHP Academy