What are the best practices for validating email inputs in PHP registration forms to prevent invalid addresses?

Validating email inputs in PHP registration forms is crucial to prevent users from entering invalid or fake email addresses. One of the best practices is to use PHP's built-in filter_var function with the FILTER_VALIDATE_EMAIL filter to check if the email address is in a valid format. Additionally, you can also perform a DNS check to verify if the domain of the email address exists.

$email = $_POST['email'];

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