PHP filter_var() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filter

Definition

In PHP, filter_var() checks or cleans a single value, such as an email address, a number, or a URL, using a filter you choose.

It is the most common way to validate user input. You give it the value and a filter like FILTER_VALIDATE_EMAIL, and it returns the value if it is good or false if it is not.

Syntax

Call filter_var() with the values you need. Start with this line.

PHP
$email = filter_var('[email protected]', FILTER_VALIDATE_EMAIL);
CallWhat it does
filter_var('[email protected]', FILTER_VALIDATE_EMAIL)Returns the email string because it is valid.
filter_var('42', FILTER_VALIDATE_INT)Returns the integer 42. A value like '4x' would return false.
filter_var('jo(hn)@example.com', FILTER_SANITIZE_EMAIL)Removes illegal characters and returns [email protected].

Validate filters (FILTER_VALIDATE_*) return the value or false. Sanitize filters (FILTER_SANITIZE_*) remove unwanted characters and always return a string. FILTER_DEFAULT leaves the value as it is.

Library declarations
PHP
filter_var(mixed $value, int $filter = FILTER_DEFAULT, array|int $options = 0): mixed

Parameters

ParameterDescription
valueRequired. The value to filter. Arrays are only filtered when you add the FILTER_REQUIRE_ARRAY or FILTER_FORCE_ARRAY flag.
filterOptional. The filter ID to apply, such as FILTER_VALIDATE_EMAIL. The default is FILTER_DEFAULT.
optionsOptional. An array with 'options' and 'flags' keys, or a single flags value such as FILTER_NULL_ON_FAILURE. The default is 0.

Return Value

Return ValueDescription
mixedThe filtered value on success. Its type depends on the filter, for example int for FILTER_VALIDATE_INT or bool for FILTER_VALIDATE_BOOLEAN.
falseReturned when a validate filter fails. With FILTER_NULL_ON_FAILURE, failure returns null instead.

PHP Examples

Example 1 — Validate an Email Address

Check two values with FILTER_VALIDATE_EMAIL. A valid email is returned as is, and an invalid one gives false.

PHP
<?php
$good = filter_var('[email protected]', FILTER_VALIDATE_EMAIL);
$bad = filter_var('user@@example', FILTER_VALIDATE_EMAIL);

echo "Good: ", $good !== false ? $good : "invalid", PHP_EOL;
echo "Bad: ", $bad !== false ? $bad : "invalid", PHP_EOL;

How It Works

  1. filter_var() receives the text and FILTER_VALIDATE_EMAIL as the filter.
  2. The first email has a normal format, so the same string is returned.
  3. The second one has two @ signs, so the filter returns false.
  4. We compare with !== false, then print either the email or the word invalid.

Example 2 — Zero Is Valid but Falsy

Some valid results look like failures in a plain if. Compare with === false to be safe.

PHP
<?php
$zero = filter_var('0', FILTER_VALIDATE_INT);
$text = filter_var('42abc', FILTER_VALIDATE_INT);
$maybe = filter_var('maybe', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);

echo "Zero: ", var_export($zero, true), PHP_EOL;
echo "42abc: ", var_export($text, true), PHP_EOL;
echo "maybe: ", var_export($maybe, true), PHP_EOL;

How It Works

  1. '0' is a valid whole number, so the integer 0 comes back. A plain if would treat it as a failure.
  2. '42abc' is not a clean integer, so FILTER_VALIDATE_INT returns false.
  3. 'maybe' is not a boolean word, and FILTER_NULL_ON_FAILURE makes the failure return null.
  4. var_export() shows each exact value so you can tell 0, false, and null apart.

Example 3 — Sanitize and Check a Range

Clean an email with FILTER_SANITIZE_EMAIL, then validate an age with a minimum and maximum value.

PHP
<?php
$email = filter_var('jo(hn)@exa mple.com', FILTER_SANITIZE_EMAIL);
echo "Clean email: ", $email, PHP_EOL;

$options = ['options' => ['min_range' => 18, 'max_range' => 65]];

foreach (['30', '17', 'abc'] as $input) {
    $age = filter_var($input, FILTER_VALIDATE_INT, $options);
    echo "Age $input: ", $age !== false ? "OK ($age)" : "not allowed", PHP_EOL;
}

How It Works

  1. FILTER_SANITIZE_EMAIL removes the parentheses and the space, which are not allowed in an email.
  2. The options array sets min_range to 18 and max_range to 65.
  3. 30 is a whole number inside the range, so it is returned as an integer.
  4. 17 is below the minimum and 'abc' is not a number, so both return false.

Frequently Asked Questions

It validates or sanitizes one value using a filter. Validate filters return the value or false, and sanitize filters strip unwanted characters from the value.
Use filter_var($email, FILTER_VALIDATE_EMAIL). It returns the email string if the format is valid and false if it is not. Remember to compare with !== false.
Validating checks the value and rejects it when it is wrong. Sanitizing quietly removes bad characters and returns what is left, even if the result is not what you wanted. Validate first when you can.
It does not. filter_var('0', FILTER_VALIDATE_INT) returns the integer 0, and filter_var('off', FILTER_VALIDATE_BOOLEAN) returns the boolean false. These are valid but falsy, so use === false or FILTER_NULL_ON_FAILURE to tell them from real failures.
No. FILTER_DEFAULT (FILTER_UNSAFE_RAW) returns the value unchanged. Use a validate filter, and escape text with htmlspecialchars() when you print it in HTML.

Next: filter_var_array()

filter_var_array() filters many values from an array at once, and each key can have its ow.

filter_var_array() tutorial →

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