PHP | filter_var() Function Last Updated : 18 Nov, 2019 Comments Improve Suggest changes Like Article Like Report The filter_var() function filters a variable with the specified filter. This function is used to both validate and sanitize the data. Syntax :- filter_var(var, filtername, options) Parameters: This function accepts three parameters and are described below: var : It is the required field. It denotes the variable to filter. filtername : It is used to specify the ID or name of the filter to use. Default is FILTER_DEFAULT, which results in no filtering. It is optional field. options : It is used to specify one or more flags/options to use. Check each filter for possible options and flags. It is also optional field. Return Value: It returns the filtered data on success, or FALSE on failure. Below are some different applications of filter_var() function: Sanitize a string : In the below example we sanitize a string Example:- PHP <?php $str = "<h1>GeeksforGeeks!</h1>"; $newstr = filter_var($str, FILTER_SANITIZE_STRING); echo $newstr; ?> Output :- GeeksforGeeks! Validate an Integer : The below example uses the filter_var() function to check if the variable $int is an integer. If $int is an integer, the output of the code below will be: "Integer is valid". If $int is not an integer, the output will be: "Integer is not valid": Example:- PHP <?php $int = 200; if (filter_var($int, FILTER_VALIDATE_INT) === 0 || !filter_var($int, FILTER_VALIDATE_INT) === false) { echo("Integer is valid"); } else { echo("Integer is not valid"); } ?> Output :- Integer is valid Validate an IP Address : The following example uses the filter_var() function to check if the variable $ip is a valid IP address: Example :- PHP <?php $ip = "129.0.0.1"; if (!filter_var($ip, FILTER_VALIDATE_IP) === false) { echo("$ip is a valid IP address"); } else { echo("$ip is not a valid IP address"); } ?> Output :- 129.0.0.1 is a valid IP address Sanitize and Validate an Email Address : The following example uses the filter_var() function to first remove all illegal characters from the $email variable, then check if it is a valid email address: Example :- PHP <?php $email = "[email protected]"; // Remove all illegal characters from email $email = filter_var($email, FILTER_SANITIZE_EMAIL); // Validate e-mail if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) { echo("$email is a valid email address"); } else { echo("$email is not a valid email address"); } ?> Output :- [email protected] is a valid email address Sanitize and Validate a URL : The following example uses the filter_var() function to first remove all illegal characters from a URL, then check if $url is a valid URL: Example :- PHP <?php $url = "https://www.geeksforgeeks.org"; // Remove all illegal characters from a url $url = filter_var($url, FILTER_SANITIZE_URL); // Validate url if (!filter_var($url, FILTER_VALIDATE_URL) === false) { echo("$url is a valid URL"); } else { echo("$url is not a valid URL"); } ?> Output :- https://www.geeksforgeeks.org is a valid URL Reference: http://php.net/manual/en/function.filter-var.php Comment More infoAdvertise with us Next Article PHP | filter_var() Function S Shivani2609 Follow Improve Article Tags : Misc Web Technologies PHP PHP-basics PHP-function +1 More Practice Tags : Misc Similar Reads PHP | filter_has_var() function The filter_has_var() function is an inbuilt function in PHP which is used to check whether the variable available or not especially it checks if a variable of a specified input type exist or not. It returns True on success or False on failure. Syntax: bool filter_has_var( $type, $variable_name ) Par 2 min read PHP | preg_filter() Function The preg_filter() function is an inbuilt function in PHP which is used to perform a regular expression search and replace the text. Syntax: preg_filter( $pattern, $replacement, $subject, $limit, $count ) Parameters: This function accepts five parameters as mention above and describe below. $pattern: 2 min read PHP | filter_id() Function The filter_id() function is an inbuilt function in PHP which returns the filter ID of a specified filter name. It is used to get the filter id of the particular filter in PHP by using filter_id function by giving the name of the filter as input and get the associated id to it. Syntax: int filter_id( 2 min read PHP | var_export() Function The var_export() is a built-in function in PHP which is used to return the structured value(information) of a variable that is passed to this function as a parameter. This function is similar to the var_dump() function.Syntax: var_export($var, $return) Parameters: This function accepts two parameter 1 min read PHP | filter_list() Function The filter_list() function is an inbuilt function in PHP which is used to returns the list of all supported filters. Syntax: array filter_list( void ) Parameters: This function does not accepts any parameters. Return Values: It returns an array containing all names of supported filters. If it return 2 min read Like