How can email validation be improved in PHP registration forms?

Email validation in PHP registration forms can be improved by using PHP's built-in filter_var function with the FILTER_VALIDATE_EMAIL flag. This function will check if the email address provided is in a valid format. Additionally, you can also perform a DNS check to ensure that the domain part of the email address exists.

$email = $_POST['email'];

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // Perform DNS check
    list($user, $domain) = explode('@', $email);
    if (checkdnsrr($domain, 'MX')) {
        // Email is valid
    } else {
        // Invalid email domain
    }
} else {
    // Invalid email format
}