$email = "user@example.com"; $domain = substr(strrchr($email, "@"), 1); if (filter_var($email, FILTER_VALIDATE_EMAIL) && checkdnsrr($domain, "MX")) echo "Valid email and domain has mail server"; else echo "Invalid email or domain has no MX records";
<form method="post"> <label>Email:</label> <input type="email" name="email" value="<?= htmlspecialchars($email) ?>" required> <?php if ($error): ?> <p style="color: red;"><?= $error ?></p> <?php endif; ?> <?php if ($success): ?> <p style="color: green;"><?= $success ?></p> <?php endif; ?> <button type="submit">Validate</button> </form> | Method | Pros | Cons | Use Case | |--------|------|------|----------| | filter_var() | Fast, standard-compliant | No domain check | General validation | | DNS check ( checkdnsrr ) | Verifies domain exists | Slower, can fail | Registration forms | | SMTP verification | Confirms user existence | Slow, often blocked | High-security needs | | Regex | Customizable | Error-prone, complex | Legacy systems only | validate email address php
While filter_var() is preferred, regex can be useful for custom rules: $email = "user@example