How to Validate an email in PHP


The filter_var() function used to validate and sanitize a variable with a specified filter. First remove all illegal characters using FILTER_SANITIZE_EMAIL and validate email address using FILTER_VALIDATE_EMAIL. The following example shows, how to validate email address in PHP.

Example:

<?php
  $email = "sample@domain.com";

  #Remove all illegal characters
  $email = filter_var($email, FILTER_SANITIZE_EMAIL);

  #Check email is Valid or Not
  if(filter_var($email, FILTER_VALIDATE_EMAIL)){
    echo $email." is valid email address";
  }else{
    echo $email." is invalid email address";
  }
?>

Output:

sample@domain.com is valid email address