PHP Filter

Beginner
6 min read
Updated: Nov 2026
3 examples
Validation & sanitizing

Definition and Usage

The PHP filter extension checks and cleans data that comes from users or other outside sources. It can validate a value (is this a real email, an integer, a URL?) or sanitize it (remove characters you do not want).

Two functions do most of the work: filter_var() filters a value you already have, and filter_input() reads a value from $_GET, $_POST, $_COOKIE, $_SERVER, or $_ENV and filters it in one step. Both use FILTER_* constants to choose the rule.

This page explains the ideas. The Filter Functions index lists all seven function guides.

Validate first.

FILTER_VALIDATE_* returns the value on success and false on failure, so you can reject bad input. FILTER_SANITIZE_* changes the value instead of rejecting it.

Filtering is not escaping.

A valid email can still hold characters that matter in HTML or SQL. Escape on output with htmlspecialchars() and use prepared statements for databases.

PHP Syntax

The two common entry points:

PHP
// Filter a value you already have
$result = filter_var($value, FILTER_VALIDATE_EMAIL);

// Read from an input source and filter in one step
$page = filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT);

// $result / $page:
//   the filtered value on success
//   false  when validation fails
//   null   (filter_input only) when the variable is not set

Core Ideas

What to remember when you filter input in PHP:

IdeaExampleRemember
ValidateFILTER_VALIDATE_EMAIL, FILTER_VALIDATE_INT, FILTER_VALIDATE_URLReturns the value or false
SanitizeFILTER_SANITIZE_NUMBER_INT, FILTER_SANITIZE_SPECIAL_CHARSChanges the value; never returns false for bad text
Value in a variablefilter_var()Works on any string, number, or array element
Value from a requestfilter_input(INPUT_POST, 'email', ...)null means missing, false means invalid
Options & flags['options' => ['min_range' => 1]], FILTER_NULL_ON_FAILUREPass them as the third argument
Many valuesfilter_var_array(), filter_input_array()One definition for a whole form

PHP Examples

Three short scripts that show how the filter extension behaves.

Example 1 — Validate an email, an integer, and a URL

Valid values come back unchanged; invalid values return false.

PHP
<?php
var_dump(filter_var('[email protected]', FILTER_VALIDATE_EMAIL));
var_dump(filter_var('not-an-email', FILTER_VALIDATE_EMAIL));

var_dump(filter_var('25', FILTER_VALIDATE_INT, [
    'options' => ['min_range' => 18, 'max_range' => 65],
]));
var_dump(filter_var('7', FILTER_VALIDATE_INT, [
    'options' => ['min_range' => 18, 'max_range' => 65],
]));

var_dump(filter_var('https://codetofun.com', FILTER_VALIDATE_URL));
?>

How It Works

  1. FILTER_VALIDATE_EMAIL returns the string when it looks like an email, otherwise false.
  2. FILTER_VALIDATE_INT returns a real int, and min_range / max_range reject numbers outside the limits.
  3. FILTER_VALIDATE_URL checks the URL format, including its scheme.

Example 2 — Sanitize text and read booleans

Strip characters from a value, and turn friendly words into real booleans.

PHP
<?php
var_dump(filter_var('a1b2c3', FILTER_SANITIZE_NUMBER_INT));

var_dump(filter_var('yes', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));
var_dump(filter_var('maybe', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));
?>

How It Works

  1. FILTER_SANITIZE_NUMBER_INT keeps only digits and the signs + and -.
  2. FILTER_VALIDATE_BOOLEAN accepts 1, true, on, and yes as true.
  3. FILTER_NULL_ON_FAILURE returns null for unknown words, so you can tell “false” from “not a boolean”.

Example 3 — filter_var() vs filter_input()

Check whether a request variable exists, then read and filter it. Run from the command line, no query string is set.

PHP
<?php
echo 'has page: ' . (filter_has_var(INPUT_GET, 'page') ? 'yes' : 'no') . "\n";

var_dump(filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT));

$page = filter_var('3', FILTER_VALIDATE_INT);
var_dump($page);
?>

How It Works

  1. filter_has_var() tells you whether the request variable was sent at all.
  2. filter_input() returns null for a missing variable, and false when the value fails the filter.
  3. filter_var() filters a value you already hold, so it never returns null for a missing variable.

Next: PHP Filter Functions Index

After you know what the filter extension does, open the function index for all seven guides.

Start withWhy
filter_var()Validate or sanitize any value
filter_input()Read and filter GET, POST, and cookies
filter_var_array()Filter many values with one definition
filter_list()See every filter name PHP supports

Full list: PHP Filter Functions index (/php/filter/function).

Frequently Asked Questions

The filter extension is a built-in set of functions and FILTER_* constants that validate and sanitize data. You can check that a value is a valid email, integer, URL, IP address, or boolean, or strip unwanted characters from it before you use it.
Validating (FILTER_VALIDATE_*) checks whether a value is acceptable and returns the value or false. Sanitizing (FILTER_SANITIZE_*) changes the value by removing or encoding characters. Validate when you want to reject bad input; sanitize when you must keep the input but make it safe for a given use.
Use filter_var() for any value you already hold in a variable. Use filter_input() to read and filter a value straight from GET, POST, COOKIE, SERVER, or ENV in one step. filter_input() returns null when the variable is missing and false when the filter fails.
No. FILTER_SANITIZE_STRING and FILTER_SANITIZE_STRIPPED are deprecated since PHP 8.1. Use FILTER_SANITIZE_SPECIAL_CHARS or htmlspecialchars() when you output text in HTML, and prepared statements for databases.
Practice the examples here, then open the Filter Functions index for all seven function guides, from filter_has_var() to filter_var_array().

Browse every Filter function

Open the functions index for categorized guides with examples and FAQs.

Filter Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful