Hindi Songs

Search About PHP!!!

Wednesday, July 8, 2009

Functions and Filters

To filter a variable, use one of the following filter functions:

  • filter_var() - Filters a single variable with a specified filter
  • filter_var_array() - Filter several variables with the same or different filters
  • filter_input - Get one input variable and filter it
  • filter_input_array - Get several input variables and filter them with the same or different filters

In the example below, we validate an integer using the filter_var() function:

< ?php
$int = 123;

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

The code above uses the "FILTER_VALIDATE_INT" filter to filter the variable. Since the integer is valid, the output of the code above will be: "Integer is valid".

If we try with a variable that is not an integer (like "123abc"), the output will be: "Integer is not valid".

For a complete list of functions and filters, visit our PHP Filter Reference.