Skip to content

Chapter 27 of 38

Input Validation

Apply boundary schemas, allowlists, normalization, filters, and useful error messages.

38 minutes 10 quick checksBy Subha Prasad
Lesson 27 of 38Course navigation

Lesson content

Read, practise, then check your understanding

Validation asks whether input satisfies the domain; sanitization transforms it. Validate at the boundary and retain a structured error list for clients.

Practical example

<?php
$email = filter_var($rawEmail, FILTER_VALIDATE_EMAIL);
$role = in_array($rawRole, ['viewer', 'editor'], true) ? $rawRole : null;
if ($email === false || $role === null) { throw new InvalidArgumentException('Invalid input'); }

Use allowlists for finite choices, explicit lengths/ranges, Unicode-aware rules, and cross-field validation. Sanitization is not a substitute for output encoding or SQL parameters. Avoid silent truncation that changes meaning.

Knowledge check

Answer every question correctly to complete this chapter.

Which statement best describes validation?
Which PHP term matches this description: Checking whether input satisfies a domain contract.
Which statement best describes sanitization?
Which PHP term matches this description: Transforming input into a constrained representation.
Which statement best describes allowlist?
Which PHP term matches this description: Acceptance limited to explicitly permitted values or forms.
Which statement best describes filter_input?
Which PHP term matches this description: Reading and validating selected external input.
Which statement best describes boundary validation?
Which PHP term matches this description: Checking untrusted data when it enters the application.

0 of 10 checks passed

Your progress is saved on this device.