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";
echo "No false book_id validation failed";
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 = '';
echo " Yes true,  email  validation passed";
echo "No false, email  validation failed";
Yes true, email validation passed
We can replace the email filter name with email filter ID ( 274 ) , just change the above line with this

Validate Boolean variable

$var1 = true; // Change the value to true or false  
echo " Yes true,  Boolean  validation passed";
echo "No false, Boolean   validation failed";
Yes true, Boolean validation passed
We can set the value based on value.
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


* indicates required
Subscribe to plus2net

    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 All rights reserved worldwide Privacy Policy Disclaimer