filter_var
(PHP 5 >= 5.2.0, PHP 7, PHP 8)
Filters a variable with a specified filter
Description
Filter a variable using a FILTER_VALIDATE_* validation filters, a FILTER_SANITIZE_* sanitization filters, or a custom filter.
Parameters
valueValue to filter.
WarningScalar values are converted to string internally before they are filtered.
filterThe filter to apply. Can be a validation filter by using one of the
FILTER_VALIDATE_*constants, a sanitization filter by using one of theFILTER_SANITIZE_*orFILTER_UNSAFE_RAW, or a custom filter by usingFILTER_CALLBACK.NoteThe default is
FILTER_DEFAULT, which is an alias ofFILTER_UNSAFE_RAW. This will result in no filtering taking place by default.optionsEither an associative
arrayof options, or a bitmask of filter flag constantsFILTER_FLAG_*.If the
filteraccepts options, flags can be provided by using the"flags"field of array.
Return Values
On success returns the filtered data. On failure false is returned, unless the FILTER_NULL_ON_FAILURE flag is used, in which case null is returned.
Examples
A filter_var() example
<?php
var_dump(filter_var('bob@example.com', FILTER_VALIDATE_EMAIL));
var_dump(filter_var('https://example.com', FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED));
?>The above example will output:
string(15) "bob@example.com"
bool(false)Example validating entries of an array
<?php
$emails = [
"bob@example.com",
"test@example.local",
"invalidemail"
];
var_dump(filter_var($emails, FILTER_VALIDATE_EMAIL, FILTER_REQUIRE_ARRAY));
?>The above example will output:
array(3) {
[0]=>
string(15) "bob@example.com"
[1]=>
string(18) "test@example.local"
[2]=>
bool(false)
}Example of passing an array for options
<?php
$options = [
'options' => [
'min_range' => 10,
],
'flags' => FILTER_FLAG_ALLOW_OCTAL,
];
var_dump(filter_var('0755', FILTER_VALIDATE_INT, $options));
var_dump(filter_var('011', FILTER_VALIDATE_INT, $options));
?>The above example will output:
int(493)
bool(false)Providing flags either directly or via an array
<?php
$str = 'string';
var_dump(filter_var($str, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));
var_dump(filter_var($str, FILTER_VALIDATE_BOOLEAN, ['flags' => FILTER_NULL_ON_FAILURE]));
?>The above example will output:
NULL
NULLCorrectly checking the result of validation
<?php
$value = '0';
// Wrong: 0 is a valid integer, but it is falsy
if (!filter_var($value, FILTER_VALIDATE_INT)) {
echo "Wrong check: '$value' rejected\n";
}
// Correct: compare strictly against false
if (filter_var($value, FILTER_VALIDATE_INT) === false) {
echo "'$value' is not a valid integer\n";
} else {
echo "'$value' is a valid integer\n";
}
?>The above example will output:
Wrong check: '0' rejected
'0' is a valid integerChecking the result of boolean validation
<?php
foreach (['yes', 'no', 'maybe'] as $value) {
$result = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
// false is a valid result here, so check for null instead
if ($result === null) {
echo "'$value' is not a valid boolean\n";
} else {
echo "'$value' is ", var_export($result, true), "\n";
}
}
?>The above example will output:
'yes' is true
'no' is false
'maybe' is not a valid booleanSee Also
filter_var_array()filter_input()filter_input_array()- Validation filters
FILTER_VALIDATE_* - Sanitization filters
FILTER_SANITIZE_*