What it is and why to use it
filter_var() applies a selected filter to a value already stored in a variable. That makes it useful for validating command-line settings, decoded data or test fixtures without reading a particular HTTP input source. Unlike filter_input(), it works on the supplied variable itself. Validation checks whether a value follows a rule; it does not prove ownership, permissions or trustworthiness.
How it works
Always choose an explicit validation filter. FILTER_VALIDATE_INT can return an integer including zero, while failure normally returns false. Therefore use === false instead of a truthiness check. Options can constrain an accepted integer range. FILTER_VALIDATE_EMAIL checks the supported email-address format but does not prove the mailbox exists. The default FILTER_DEFAULT is an alias of FILTER_UNSAFE_RAW and does no filtering.
Common mistakes and limitations
Do not assume the default filter cleans data. Do not use deprecated FILTER_SANITIZE_STRING or silently change invalid values into valid-looking ones. Check that request fields have the expected scalar shape, validate them for their intended purpose, and still escape output or bind SQL parameters at the relevant boundary.
Run the examples locally
Save each complete example in its own example.php file and run php example.php in a terminal from that folder. Each example is independent: do not paste both into one file, especially when they declare classes with the same names. Output appears in the terminal. The examples use PHP 8.0-compatible syntax; use a currently supported PHP release for development.
The <?php opening tag starts PHP code. declare(strict_types=1) requests strict scalar argument checks for function calls made from this file; it does not validate external input for you. echo writes output, and PHP_EOL adds the platform’s line ending. Quotation marks delimit strings; they are not part of the printed text unless explicitly included in the string.
These examples use built-in PHP language features and standard extensions. They need no Composer packages, database server, browser APIs or network access. Codectionary’s PHP preview does not execute PHP; use your local interpreter to reproduce the output.
Check your interpreter, lint and run one saved example
php -v
php -l example.php
php example.phpContinue learning
Use the related guides to connect this operation to the surrounding language features. Practise by changing the example inputs and predicting the output before running it.
Syntax
$value = filter_var($input, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]]);Examples
Accept a bounded numeric setting, including zero
Validate a retry count without accidentally rejecting a valid zero.
<?php
declare(strict_types=1);
$input = '0';
$retries = filter_var($input, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0, 'max_range' => 5]]);
if ($retries === false) {
echo 'Invalid retry count', PHP_EOL;
} else {
echo 'Retries: ', $retries, PHP_EOL;
}
- The filter recognises a valid integer representation within the inclusive range 0 to 5.
- The result is integer 0, so a simple if (!$retries) would confuse valid input with failure.
- The strict comparison accepts zero and prints the setting. A configuration loader can use the validated integer directly.
- Expected output: Retries: 0
Compare email validation with the default filter
See why a chosen validation rule matters.
<?php
declare(strict_types=1);
$candidate = 'not-an-email';
$validated = filter_var($candidate, FILTER_VALIDATE_EMAIL);
$unchanged = filter_var($candidate);
echo $validated === false ? 'Invalid format' : 'Accepted format', PHP_EOL;
echo $unchanged, PHP_EOL;
- FILTER_VALIDATE_EMAIL rejects the sample and returns false.
- Calling filter_var with no filter simply returns this string unchanged because the default is UNSAFE_RAW.
- An accepted email format would still need a confirmation workflow if the application must establish mailbox control.
- Expected output: Invalid format not-an-email
Best practices
- Use explicit validation filters and strict failure comparisons.
- Separate format validation from authorisation and output handling.
Runtime & versions
PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
PHP Manual