← Back to PHP Course | Chapter 10: Forms & Validation | Lesson 6 of 8

PHP Filters

Filters are like a security guard checking things at a door: they decide if a value, such as an email or number, is allowed in. PHP can also clean the value so it is safe.
Syntax
php
$result = filter_var($value, FILTER_VALIDATE_EMAIL);   // value, or false
$result = filter_var($value, FILTER_VALIDATE_INT);
$result = filter_var($value, FILTER_VALIDATE_URL);
$result = filter_var($value, FILTER_VALIDATE_BOOLEAN);

What is filter_var()?

filter_var() validates or sanitizes a single value against a named filter in one function call, returning the cleaned value on success or false on failure.

It centralizes validation logic that would otherwise be scattered across regular expressions and manual checks.

Example: What is filter_var()?

php
<?php
// Declare `$email`, set to "[email protected]"
$email = "[email protected]";
// Declare `$result`, set to `filter_var($email, FILTER_VALIDATE_EMAIL)`
$result = filter_var($email, FILTER_VALIDATE_EMAIL);
// Print `$result` to the output
echo $result;
?>

Validating Email and Integers

FILTER_VALIDATE_EMAIL checks a string against the structural rules of a valid email address, and FILTER_VALIDATE_INT confirms a value is a genuine integer — both return false on failure rather than throwing, so the result must always be checked with === false, not just falsy checks.

Example: Validating Email and Integers

php
<?php
// Print a detailed dump (with types) of `filter_var("[email protected]", FILTER_VALIDATE_EMAIL) === false`
var_dump(filter_var("[email protected]", FILTER_VALIDATE_EMAIL) === false);
// Print a detailed dump (with types) of `filter_var("42", FILTER_VALIDATE_INT) === false`
var_dump(filter_var("42", FILTER_VALIDATE_INT) === false);
?>

Validating URLs and Booleans

FILTER_VALIDATE_URL confirms a string is a well-formed URL, while FILTER_VALIDATE_BOOLEAN interprets common truthy/falsy strings like "yes", "1", and "off" into a real boolean value, which is more forgiving than a plain PHP boolean cast.

Example: Validating URLs and Booleans

php
<?php
// Print a detailed dump (with types) of `filter_var("https://example.com", FILTER_VALIDATE_URL)`
var_dump(filter_var("https://example.com", FILTER_VALIDATE_URL));
// Print a detailed dump (with types) of `filter_var("yes", FILTER_VALIDATE_BOOLEAN)`
var_dump(filter_var("yes", FILTER_VALIDATE_BOOLEAN));
?>

Sanitizing Input

Sanitizing filters like FILTER_SANITIZE_STRING (or its modern replacement using htmlspecialchars) and FILTER_SANITIZE_EMAIL strip or encode unwanted characters from a value rather than rejecting it outright — useful when you want to clean input for safe display, not just verify its shape.

Example: Sanitizing Input

php
<?php
// Declare `$comment`, set to "<b>Hello</b>"
$comment = "<b>Hello</b>";
// Print `htmlspecialchars($comment) . "\n"` to the output
echo htmlspecialchars($comment) . "\n";
// Print `filter_var("user@ example.com", FILTER_SANITIZE_EMAIL)` to the output
echo filter_var("user@ example.com", FILTER_SANITIZE_EMAIL);
?>

Why Filtering Matters for Untrusted Input

Any data coming from $_GET, $_POST, or $_COOKIE should be treated as untrusted until proven otherwise, since a user (or an attacker) fully controls what's sent.

Filtering at the point of entry catches malformed or malicious data before it ever reaches a database query or gets echoed back into HTML.

Example: Why Filtering Matters for Untrusted Input

php
<?php
// Set `$_GET['id']` to "5 OR 1=1"
$_GET['id'] = "5 OR 1=1";
// Declare `$id`, set to `filter_var($_GET['id'], FILTER_VALIDATE_INT)`
$id = filter_var($_GET['id'], FILTER_VALIDATE_INT);
// Print a detailed dump (with types) of `$id`
var_dump($id);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Testing the result of filter_var with a loose check, so a valid 0 looks like failure; compare with false.
  2. Expecting FILTER_VALIDATE_BOOLEAN to return false for invalid input, when it needs FILTER_NULL_ON_FAILURE.
  3. Using a sanitize filter when a validate filter is needed.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.