Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Functions and Classes

PHP filter_var() function

Reference for PHP filter_var(): validate integers, floats, booleans, emails, URLs and IPs, set ranges and defaults, sanitize text and handle failure values.

filter_var() checks a value against a built-in filter and returns it converted to the right type, or false when it is not valid. There are validation filters (is this an integer? an email address?) and sanitizing filters that strip or encode unwanted characters.

Validation is the safe default for untrusted input: the function returns a clean value or a clear failure. Because the failure value is false, a legitimate false result is easy to confuse with an error, so the examples below show how to tell them apart.

PHP
$age = filter_var($input, FILTER_VALIDATE_INT, ["options" => ["min_range" => 0, "max_range" => 120]]);
if ($age === false) {
    echo "Invalid age";
}

Try it

PHP Validating integers, floats and booleans

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Emails, URLs, IP addresses and sanitizing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
filter_var(mixed $value, int $filter = FILTER_DEFAULT, array|int $options = 0): mixed
Name Description
$value The value to filter. Scalars are converted to strings first; arrays need FILTER_REQUIRE_ARRAY or FILTER_FORCE_ARRAY.
$filter The filter constant, such as FILTER_VALIDATE_INT, FILTER_VALIDATE_FLOAT, FILTER_VALIDATE_BOOLEAN (alias FILTER_VALIDATE_BOOL), FILTER_VALIDATE_EMAIL, FILTER_VALIDATE_URL, FILTER_VALIDATE_IP, FILTER_VALIDATE_DOMAIN, FILTER_VALIDATE_MAC, FILTER_VALIDATE_REGEXP, FILTER_SANITIZE_FULL_SPECIAL_CHARS, FILTER_SANITIZE_SPECIAL_CHARS, FILTER_SANITIZE_NUMBER_INT, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_SANITIZE_EMAIL, FILTER_SANITIZE_URL or FILTER_CALLBACK. The default FILTER_DEFAULT (FILTER_UNSAFE_RAW) does nothing but convert to string.
$options Either an integer of flags or an array with "options" (filter settings such as min_range, max_range, default, regexp, or the callable for FILTER_CALLBACK) and "flags".

Return value: the filtered value (an int for FILTER_VALIDATE_INT, a float for FILTER_VALIDATE_FLOAT, a bool for the boolean filter, otherwise a string), or false on failure. With FILTER_NULL_ON_FAILURE, failure returns null instead, which lets the boolean filter separate "not a boolean" (null) from "false" (false).

Errors: an unknown filter constant produces a warning and returns false. Invalid input never throws.

Usage notes

  • Compare with === false. filter_var("0", FILTER_VALIDATE_INT) returns 0, which is falsy but valid, so if (!$result) is a bug.
  • Integers are strict. Surrounding whitespace is trimmed, but "42.0" and "007" fail. A leading + or - is accepted. Hex and octal need FILTER_FLAG_ALLOW_HEX or FILTER_FLAG_ALLOW_OCTAL.
  • Defaults and ranges go in the "options" array: min_range, max_range and default (returned instead of false).
  • The boolean filter accepts "1", "true", "on" and "yes" as true, and "0", "false", "off", "no" and "" as false, case-insensitively. Anything else is false unless you add FILTER_NULL_ON_FAILURE.
  • Email checks syntax only. It does not prove the mailbox exists, and by default it rejects non-ASCII local parts (use FILTER_FLAG_EMAIL_UNICODE). Send a confirmation link to verify an address.
  • URL validation is loose. It needs a scheme ("example.com" fails) but accepts schemes other than http and https (for example ftp:); check the scheme yourself before using a URL in a link.
  • Sanitizing is not escaping for output. To print user text in HTML use htmlspecialchars(). FILTER_SANITIZE_STRING was deprecated in PHP 8.1; do not use it.
  • For many fields use filter_var_array() with one definition per key, or filter_input() to read straight from the request.
  • FILTER_VALIDATE_REGEXP needs the regexp option and returns the original string on a match.