Syntax:filter_input ($type, $variable_name, $filter, $options)
Parameters:this the function takes four parameters as above and described below:
- $type:this is a required parameter used to validate the input type. Filter list:
- INPUT_GET
- INPUT_POST
- INPUT_COOKIE
- INPUT_SERVER
- INPUT_ENV
- $variable_name:Required. It is used to store the name of the variable to be checked.
- $filter:This is an optional parameter. It contains the name or ID of the filter. If this parameter is not set, then FILTER_DEFAULT is used.
- $options:is an optional parameter used to specify one or more flags / options to use. It checks for possible choices and flags in each filter. If the filter parameters are accepted, the flags can be specified in the "flags" field of the array.
Returned value:returns the value of the variable on success, or False on error. If the parameter is not set, return NULL. If the FILTER_NULL_ON_FAILURE flag is used, it returns FALSE if the variable is not set and NULL if the filter fails.
Example 1:
// PHP program for checking mail using a filter
if
(isset (
$_ GET
[
"email"
])) {
if
(! filter_input (INPUT_GET ,
"email"
,
FILTER_VALIDATE_EMAIL) === false) {
echo
(
" Valid Email "
);
}
else
{
echo
(
"Invalid Email"
);
}
}
?>
Output:Valid Email
Example 2:
// Input type: INPUT_GET input name: search
// filter name: FILTER_SANITIZE_SPECIAL_CHARS
$search_variable_data
= filter_input (INPUT_GET ,
’search’
, FILTER_SANITIZE_SPECIAL_CHARS );
// Input type: INPUT_GET input name: search
// filter name: FILTER_SANITIZE_ENCODED
$search_url_data
= filter_input (INPUT_GET,
’search’
, FILTER_SANITIZE_ENCODED);
echo
" Search for $search_variable_data. "
;
echo
" Search again. "
;
?>
Output:Search for tic tac & amp; toc. Search again.
Links: http://php.net /manual/en/function.filter-input.php