HomeMenu
Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

filter_var

Description

The filter_var of Filter for PHP filters a variable with a specified filter.

Syntax

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

Parameters

value

Value to filter.

WARNING: Scalar values are converted to string internally before they are filtered.

filter

The filter to apply. Can be a validation filter by using one of the FILTER_VALIDATE_* constants, a sanitization filter by using one of the FILTER_SANITIZE_* or FILTER_UNSAFE_RAW, or a custom filter by using FILTER_CALLBACK.

NOTE: The default is FILTER_DEFAULT, which is an alias of FILTER_UNSAFE_RAW. This will result in no filtering taking place by default.

options

Either an associative array of options, or a bitmask of filter flag constants FILTER_FLAG_*. If the filter accepts options, flags can be provided by using the "flags" field of array.

Return

Returns the filtered data on success or false on failure, unless the FILTER_NULL_ON_FAILURE flag is used, in which case null is returned.

Examples

1 · value · bool

<?

$value = true;

$return = filter_var($value);

var_dump($return);
string(1) "1"

2 · value · int

<?

$value = 1;

$return = filter_var($value);

var_dump($return);
string(1) "1"

3 · value · float

<?

$value = 0.1;

$return = filter_var($value);

var_dump($return);
string(3) "0.1"

4 · filter · FILTER_VALIDATE_BOOL

<?

$value = true;
$filter = FILTER_VALIDATE_BOOL;

$return = filter_var($value, $filter);

var_dump($return);
bool(true)

5 · filter · FILTER_VALIDATE_INT

<?

$value = 1;
$filter = FILTER_VALIDATE_INT;

$return = filter_var($value, $filter);

var_dump($return);
int(1)

6 · filter · FILTER_VALIDATE_FLOAT

<?

$value = 0.1;
$filter = FILTER_VALIDATE_FLOAT;

$return = filter_var($value, $filter);

var_dump($return);
float(0.1)

7 · filter · FILTER_VALIDATE_EMAIL

<?

$value = "email@domain.com";
$filter = FILTER_VALIDATE_EMAIL;

$return = filter_var($value, $filter);

echo $return;
email@domain.com

8 · filter · FILTER_VALIDATE_URL

<?

$value = "https://osbo.com";
$filter = FILTER_VALIDATE_URL;

$return = filter_var($value, $filter);

echo $return;
https://osbo.com

9 · filter · FILTER_SANITIZE_EMAIL

<?

$value = "e m a i l @ d o m a i n . c o m";
$filter = FILTER_SANITIZE_EMAIL;

$return = filter_var($value, $filter);

echo $return;
email@domain.com

10 · filter · FILTER_SANITIZE_URL

<?

$value = "h t t p s : / / o s b o . c o m";
$filter = FILTER_SANITIZE_URL;

$return = filter_var($value, $filter);

echo $return;
https://osbo.com

11 · filter · FILTER_CALLBACK

<?

function myfunction($myparameter)
{
    return $myparameter;
}

$value = "callback";
$filter = FILTER_CALLBACK;
$options =
[
    "options" => "myfunction"
];

$return = filter_var($value, $filter, $options);

echo $return;
callback

12 · options · FILTER_NULL_ON_FAILURE · bitmask

<?

$value = "invalid";
$filter = FILTER_VALIDATE_BOOL;
$options = FILTER_NULL_ON_FAILURE;

$return = filter_var($value, $filter, $options);

var_export($return);
NULL

13 · options · FILTER_NULL_ON_FAILURE · array

<?

$value = "invalid";
$filter = FILTER_VALIDATE_BOOL;
$options =
[
    "flags" => FILTER_NULL_ON_FAILURE
];

$return = filter_var($value, $filter, $options);

var_export($return);
NULL

14 · options · FILTER_FLAG_PATH_REQUIRED

<?

$value = "https://osbo.com/php/";
$filter = FILTER_VALIDATE_URL;
$options = FILTER_FLAG_PATH_REQUIRED;

$return = filter_var($value, $filter, $options);

echo $return;
https://osbo.com/php/

15 · options · FILTER_REQUIRE_ARRAY

<?

$value =
[
    "https://osbo.com",
    "h t t p s : / / o s b o . c o m",
    "invalid"
];
$filter = FILTER_VALIDATE_URL;
$options = FILTER_REQUIRE_ARRAY;

$return = filter_var($value, $filter, $options);

var_dump($return);
array(3) {
  [0]=>
  string(16) "https://osbo.com"
  [1]=>
  bool(false)
  [2]=>
  bool(false)
}

16 · options · FILTER_FLAG_ALLOW_OCTAL

<?

$value = "0o755";
$filter = FILTER_VALIDATE_INT;
$options =
[
    "flags" => FILTER_FLAG_ALLOW_OCTAL,
    "options" =>
    [
        "default" => 0,
        "min_range" => 50,
        "max_range" => 500
    ]
];

$return = filter_var($value, $filter, $options);

echo $return;
493