What are the best practices for checking the validity of email addresses in PHP?

Validating email addresses in PHP involves checking the format of the email address and verifying the domain exists. One common method is to use PHP's built-in filter_var function with the FILTER_VALIDATE_EMAIL filter. Additionally, you can use DNS validation to check if the domain has a valid MX record.

$email = "example@example.com";

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    list($user, $domain) = explode('@', $email);
    if (checkdnsrr($domain, 'MX')) {
        echo "Email address is valid.";
    } else {
        echo "Email address domain does not exist.";
    }
} else {
    echo "Invalid email address format.";
}