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:
|
| 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
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
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
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:




Join the conversation! Your thoughts help the community grow.