Filter


  • Filter use when the data source contains user input data or unknown data.

  • Validation is used to validate or restrict some user input.

  • Filter use mainly for security purpose because we can not trust on external data such as :

    1. input data from a form.
    2. cookies.
    3. Web services data.
    4. Server variables.
    5. Database query results).

Functions Description
filter_var() Filters a single variable with a specific filter.
filter_var_array() Filter more than one variables with the same or different filters.
filter_input Receive one input variable and filter it.
filter_input_array Receive more than one input variables and filter them with the same or different filters.

Example :
<?php
$int= 10000000123;

if(!filter_var($int, FILTER_VALIDATE_INT))
  {
  echo("Integer is not valid");
  }
else
  {
  echo("Integer is valid");
  }
?>
Output :
Integer is not valid

Validate Input


By using filter_input() function ,we can filter user-input.

Example :
<?php
if(!filter_has_var(INPUT_GET, "email-id"))
  {
  echo("Input type does not exist");
  }
else
  {
  if (!filter_input(INPUT_GET, "email-id", FILTER_VALIDATE_EMAIL))
    {
    echo "E-Mail id is not valid";
    }
  else
    {
    echo "E-Mail id is valid";
    }
  }
?>