filter_var(): Checks a variable with a specified filter

$book_id='abc'; // change this to integer to check
if(filter_var($book_id,257)){ // id 257 is for validating integers
echo "Yes true book_id validation passed";
}else{
echo "No false book_id validation failed";
}
output
No false book_id validation failed
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 );
You can get list of filter ID here

Validating Email

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";
}
output
Yes true, email validation passed
We can replace the email filter name with email filter ID ( 274 ) , just change the above line with this
if(filter_var($email,274)){

Validate Boolean variable

$var1 = true; // Change the value to true or false  
if(filter_var($var1,FILTER_VALIDATE_BOOLEAN)){
echo " Yes true,  Boolean  validation passed";
}else{
echo "No false, Boolean   validation failed";
}
Output
Yes true, Boolean validation passed
We can set the value based on value.
$var1=true;
$var1 = filter_var('abcd', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
echo $var1;
echo var_dump($var1); // NULL

Filter reference Validating Boolean data Ctype_alnum to check alphanumeric characters Validating Email address
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer