PHP Function Reference

PHP filter_var() Function



The PHP filter_var() function is used to filter a variable with a specified filter.

Syntax

filter_var(var, filter, options)

Parameters

value Required. Specify the variable to filter.
filter Optional. Specify the ID of the filter to apply. See the filter list. If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW. This will result in no filtering taking place by default.
options Optional. Specify an associative array of options or bitwise disjunction of flags. If filter accepts options, flags can be provided in "flags" field of array.

Return Value

Returns the filtered data, or false if the filter fails.

Example: validating a float

In the example below, filter_var() function is used to validate a given variable.

<?php
function validate_age($age) {
  $opt = 
  array(
    'options' => array(
      //value to return if the filter fails
      'default' => "Age is not valid",

      //other options here
      "min_range"=>1,
      "max_range"=>100
    )
  );

  //checking the validating of age 
  if (!filter_var($age, FILTER_VALIDATE_FLOAT, $opt) === false)
    echo filter_var($age, FILTER_VALIDATE_FLOAT, $opt)."\n";
}

//validating age
validate_age(50);
validate_age(75);
validate_age(150);
validate_age("abc");
?>

The output of the above code will be:

50
75
Age is not valid
Age is not valid

Example: sanitizing and validating an email address

In the example below, the function is used to validate an email.

<?php
function validate_email($email) {
  //removing all illegal characters from email
  $email = filter_var($email, FILTER_SANITIZE_EMAIL);
    
  //validating the email
  if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
    echo("$email is a valid email address\n");
  } else {
    echo("$email is not a valid email address\n");
  }
}

//validating emails
validate_email("abc@example.com");
validate_email("abc@example");
validate_email("abc@");
?>

The output of the above code will be:

abc@example.com is a valid email address
abc@example is not a valid email address
abc@ is not a valid email address

Example: sanitizing and validating a URL

In the example below, the function is used to validate the given URL.

<?php
function validate_URL($url) {
  //removing all illegal characters from url
  $url = filter_var($url, FILTER_SANITIZE_URL);
    
  //validating the url
  if (!filter_var($url, FILTER_VALIDATE_URL) === false) {
    echo("$url is a valid URL\n");
  } else {
    echo("$url is not a valid URL\n");
  }
}

//validating URLs
validate_URL("https://www.alphacodingskills.com");
validate_URL("alphacodingskills");
?>

The output of the above code will be:

https://www.alphacodingskills.com is a valid URL
alphacodingskills is not a valid URL

Example: validating IP addresses

In the example below, the function is used to validate IP addresses.

<?php
function validate_IP($ip) {
  //validating the IP address
  if (!filter_var($ip, FILTER_VALIDATE_IP) === false) {
    echo("IP address '$ip' is considered valid\n");
  } else {
    echo("$ip is not a valid IP address\n");
  }
}

//validating IP addresses
validate_IP("127.0.0.1");
validate_IP("42.42");
?>

The output of the above code will be:

IP address '127.0.0.1' is considered valid
42.42 is not a valid IP address

❮ PHP Filter Reference