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.


  $email = "";

  #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";
    echo $email." is invalid email address";

Output: is valid email address