filter_var()

filter_var() is a function in PHP used for filtering and validating data. It's a part of PHP's filter extension, which provides a range of functionalities for validating and sanitizing data, such as email addresses, URLs, IP addresses, and more.

This function is particularly useful for ensuring that data conforms to specific formats or ranges, making it a valuable tool for data validation in PHP applications.

The syntax is:

filter_var(value, filter, options)
  • value: The variable you want to check or filter.
  • filter: The type of check to use. PHP provides many predefined filters for various purposes, like FILTER_VALIDATE_EMAIL, FILTER_SANITIZE_STRING, etc.
  • options: An associative array of options or bitwise disjunction of flags. The available options and flags depend on the chosen filter.

Some common uses are validating user input from forms, such as email addresses, URLs, and numbers. Another use is sanitizing data before it is used in a query or output to prevent security issues like SQL injection or Cross-Site Scripting (XSS) attacks.

Some examples may be validating an email:

$email = "test@example.com";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "This is a valid email address.";
} else {
    echo "This is not a valid email address.";
}

Or validating/sanitizing a string:

$string = "<script>alert('Hi!');</script>";
$sanitizedString = filter_var($string, FILTER_SANITIZE_STRING);
echo $sanitizedString;
// Outputs: alert('Hi!');

Using filter_var() is a good practice for enhancing the security of your application by preventing the use of invalid or malicious data.