Monday 2 February 2015

filter_var PHP

filter_var

We can check a variable by using a particular type of filter by using filter_var() function. This function takes the variable and the ID or name of the filter as input and checks the variable. It returns True of False based on the checking output. Here is the syntax.
bool=filter_var(variable, Filter ID,optional );
We will try with an example with book_id as variable
if(filter_var($_GET['book_id'],257)){ // id 257 is for validating integers
echo "Yes true book_id validation passed";
}else{
echo "No false book_id validation failed";
}
You can get list of filter ID here
if(filter_var($_GET['book_id'],FILTER_VALIDATE_INT)){
echo "Yes true $book_id";
}else{
echo "No false $book_id";
}
IN the above code we have not user filter id and in that place we have used filter Name. Now let us try one email validation by using email filter
$email = 'userid@example.com';
if(filter_var($email,FILTER_VALIDATE_EMAIL)){
echo " Yes true,  email  validation passed";
}else{
echo "No false, email  validation failed";
}
We can replace the email filter name with email filter ID ( 274 ) , just change the above line with this
if(filter_var($email,274)){

0 comments:

Post a Comment