What are the advantages of using filter_var() for input validation in PHP?

When accepting user input in PHP, it is important to validate and sanitize the data to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks. One common way to achieve this is by using the filter_var() function in PHP, which allows you to easily validate input data against various filters.

// Example of using filter_var() for input validation
$email = $_POST['email'];

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // Email is valid, proceed with processing
} else {
    // Email is not valid, handle the error accordingly
}