Filter Functions in PHP

Introduction

A PHP filter validates data from an insecure source; for example user input or say it is useful when a data source contains unknown data such as data from an HTML document. Basically it contains numerous useful functions that enable you to, for example:

  • Get a variable and filter it.
  • Get multiple variables and filter it.
  • Get multiple inputs from outside the script and filter it.

So I explain each and every filter function here.

PHP filter_has_var() function

The PHP filter function filter_has_var
validates a variable for the existence of a specified type and it returns TRUE on success or FALSE on failure.

Syntax

filter_has_var(type,variable)

Parameter in filter_has_var function

The parameters of the function are:

Parameter Description
type It specifies the type to check for and its possible inputs are:
  • INPUT_GET
  • INPUT_POST
  • INPUT_COOKIE
  • INPUT_SERVER
  • INPUT_ENV
variable It specifies the variable to check.

Example

An example of the
function is:

<?php
if
(!filter_has_var(INPUT_GET, "name"))
{

echo
("Input type does not exist");
}

else

{

echo
("Input type exists");
}

?>


Output


filter-has-var-function-in-php.jpg

PHP filter_id() function

The PHP filter function filter_id
returns the filter id number of the specified filter or say it returns the id of a filter on success or false if the filter does not exist.

Syntax

filter_id(filterName)

Parameter in filter_id function

The parameters of the function is:

Parameter Description
filterName It specifies the filter to get the id from

Example

An example of the
function is:

<?
php
echo
(filter_id("validate_email"));
?>


Output


filter-id-function-in-php.jpg

PHP filter_input() function

The PHP filter filter_input
function gets a specific external variable name and optionally filters it and it gives the value of the requested variable on success or false if the filter fails.

Syntax

filter_input(inputType,variable,filter,options)

Parameter in filter_input function

The parameters of the function are:

Parameter Description
inputType It specifies the input type.
variable It specifies the variable to filter.
filter It specifies the id of the filter to apply.
options It specifies the associative array of optional or bitwise disjunction of flags.

Example

An example of the
function is:

<?php

if (!filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL))

{

echo "E-Mail is not valid";

}

else

{

echo "E-Mail is valid";

}

?>

Output

filter-input-function-in-php.jpg

PHP filter_input_array() function

The PHP filter function filter_input_array
gets external variables and optionally filters them and it returns an array containing the values of the requested variables on success or false on failure. 

Syntax

filter_input_array(inputType,filterArgs)

Parameter in filter_input_array function

The parameters of the function are:

Parameter Description
inputType It specifies the input types.
filterArgs It specifies an array of filter arguments.

Example

An example of the
function is:

<?php

$filters = array

  (

  "name" => array

    (

    "filter"=>FILTER_CALLBACK,

    "flags"=>FILTER_FORCE_ARRAY,

    "options"=>"ucwords"

    ),

  "age" => array

    (

    "filter"=>FILTER_VALIDATE_INT,

    "options"=>array

      (

      "min_range"=>1,

      "max_range"=>120

      )

    ),

  "email"=> FILTER_VALIDATE_EMAIL,

  );

print_r(filter_input_array(INPUT_POST, $filters));

?>

PHP filter_list() function

The PHP filter filter_list
function returns a list of all supported filters or say it returns an array of names of all supported filters.

Syntax

filter_list()

Example

An example of the
function is:
 

<?php

echo "<pre>";

print_r(filter_list());

?>

Output

filter-list-function-in-php.jpg

PHP filter_var_array() function

The PHP filter filter_var_array
function gets multiple variables and optionally filters them and it returns an array containing the values of the requested variable on success or false on failure.

Syntax

filter_var_array(array,args)

Parameter in filter_var_array function

The parameters of the function are:

Parameter Description
array It specifies the array with string key containing the data to filter.
args It specifies an array of filter arguments.

Example

An example of the
function is:

<?
php
$
arr = array
(

"name"
=> "Nitin",
"age" => "23",
"email" => "[email protected]",
);
 $filters = array
(
"name" => array
(
"filter"=>FILTER_CALLBACK,
"flags"=>FILTER_FORCE_ARRAY,
"options"=>"ucwords"
),
"age" => array
(
"filter"=>FILTER_VALIDATE_INT,
"options"=>array
(
"min_range"=>1,
"max_range"=>120
)
),
"email"=> FILTER_VALIDATE_EMAIL,
);
echo "
<pre>";
print_r(filter_var_array($arr, $filters));
?>


Output


filter-var-array-function-in-php.jpg

PHP filter_var() function

The PHP filter filter_var
function filters a variable with a specified filter and it returns the filtered data on success or false on failure.

Syntax

filter_var(variables,filter,options)

Parameter in filter_var function

The parameters of the function are:

Parameter Description
variable It specifies the variable to filter.
filter It specifies the ID of the filter to use.
options It specifies the associative array of options or a single option.

Example

An example of the
function is:

<?
php
if
(!filter_var("abc@gmail.com", FILTER_VALIDATE_EMAIL))
 {
 
echo("EMail is not valid");
 }

else

 {
 
echo("EMail is valid");
 }

?>

Output

filter-var-function-in-php.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all