PHP Filter_input_array Function
What is filter_input_array() in PHP?
Explanation
filter_input_array() is a function, which gets more than one external variables and filters them.
Syntax:
filter_input_array(input_type, filter_args)
In the above syntax, 'input_type' is the type in which the input is passed, 'filter_args' is an array of external variables.
Example :
<?php
$valarr = array
(
"num" => array
(
"filter"=>FILTER_VALIDATE_INT,
),
"age" => array
(
"filter"=>FILTER_VALIDATE_INT,
"options"=>array
(
"min_range"=>25,
"max_range"=>50
)
),
"email"=> FILTER_VALIDATE_EMAIL,
);
$chkarr=filter_input_array(INPUT_GET, $valarr);
$num=$chkarr['num'];
$age=$chkarr['age'];
$email=$chkarr['email'];
if($chkarr['num']=="")
echo "Given value is not a number<br>";
else
echo "Number :$num <br>";
if($chkarr['age']=="")
echo "Age is not a number or Exceeds age range<br>";
else
echo "AGE :$age <br>";
if($chkarr['email']=="")
echo "Email is not a valid<br>" ;
else
echo "Email :$email <br>";
?>
Result :
Number :123
AGE :27
Email :asdasd@asdasd.com
Filtering multiple input variables without calling filter_input() many times is possible using this function.
In the above example, the external variables Number, Age and Email is validated.
The values are passed through url for the test.php file as follows,
test.php?num=123&age=27&email=asdasd@asdasd.com Since, all the values are valid the above output is retrieved.