How to validate an email address in PHP?

Introduction:
Validating an email address ensures that it meets the correct format. PHP offers a built-in filter_var() function for this.

Example:

<?php
$email = "test@example.com";

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "Valid email address.";
} else {
    echo "Invalid email address.";
}
?>

Explanation:

  • filter_var($email, FILTER_VALIDATE_EMAIL) checks if the $email adheres to the proper email format.
  • Returns the email if valid or false if invalid.

Leave a Reply

Your email address will not be published. Required fields are marked *