PHP Filters
PHP Filters: Validating and Cleaning External Data
PHP's Filter extension provides tested, built-in rules for validating untrusted input from forms, query parameters, and API payloads. Using filter_var() and filter_input() replaces error-prone manual checks with clear, standardized flags.
Common Validation Filters with filter_var()
Every validation filter returns either the validated value on success or false on failure. Always compare strictly against false so valid falsy values like 0 or false do not trip your error checks.
| Filter Constant | Purpose |
|---|---|
FILTER_VALIDATE_INT |
Checks whole integers with optional min_range / max_range |
FILTER_VALIDATE_FLOAT |
Checks decimal numbers |
FILTER_VALIDATE_EMAIL |
Checks email address format |
FILTER_VALIDATE_URL |
Checks URL structure |
FILTER_VALIDATE_IP |
Checks IPv4 and IPv6 addresses |
FILTER_VALIDATE_BOOLEAN |
Parses "true", "1", "yes", "off" cleanly |
<?php
declare(strict_types=1);
$rawPage = "3";
$rawEmail = "student@kodersolution.com";
$rawIp = "192.168.1.25";
$page = filter_var($rawPage, FILTER_VALIDATE_INT, [
'options' => ['min_range' => 1, 'max_range' => 500],
]);
$email = filter_var($rawEmail, FILTER_VALIDATE_EMAIL);
$ip = filter_var($rawIp, FILTER_VALIDATE_IP);
var_dump($page, $email, $ip);
Reading Request Superglobals with filter_input()
filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT) reads directly from the original HTTP input instead of a potentially modified $_GET array, returning null if the parameter is missing and false if invalid.
Common Mistakes to Avoid
- Checking
if (!$intVal)afterFILTER_VALIDATE_INT: If the user enters"0",filter_var("0", FILTER_VALIDATE_INT)returns0, which evaluates as falsy inif (!$intVal). Always writeif ($intVal === false). - Using deprecated
FILTER_SANITIZE_STRING: Removed in PHP 8.2+ because stripping tags breaks legitimate text like5 < 10. Escape on output withhtmlspecialchars()instead. - Forgetting
FILTER_NULL_ON_FAILUREfor booleans: By default,FILTER_VALIDATE_BOOLEANreturnsfalseboth for"false"and invalid strings like"banana". AddFILTER_NULL_ON_FAILUREto detect invalid inputs.
Frequently Asked Questions
When should I use filter_var() instead of is_int()?
HTTP query parameters and form inputs always arrive as strings ("42"). is_int("42") returns false, whereas filter_var("42", FILTER_VALIDATE_INT) returns 42.
How does FILTER_FLAG_ALLOW_OCTAL or FILTER_FLAG_IPV6 work?
Flags customize a filter's rules—for example, FILTER_FLAG_NO_PRIV_RANGE rejects local network IPs like 192.168.1.1 when validating public server endpoints.
Can filter_var_array() validate multiple fields at once?
Yes. Pass an associative input array and a rule map to filter_var_array() to validate an entire request payload in one call.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.