PHP Code Examples

How to Validate an Email Address in PHP

Validate user-supplied email addresses with filter_var(), normalize input, and explain why verification emails are still necessary.

Example

$email = trim($_POST['email'] ?? '');

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    http_response_code(422);
    echo 'Please enter a valid email address.';
    exit;
}

echo 'The email format is valid.';

Why this works

FILTER_VALIDATE_EMAIL checks whether the value has a valid email-address format. It does not prove that the mailbox exists or belongs to the user.

For account registration, send a confirmation link and verify ownership before enabling sensitive account actions.

Practical notes

  • Treat all form input as untrusted.
  • Keep validation on the server even when browser-side validation is present.
  • Store the normalized value consistently, and consider case-handling rules for your application.
Share this page Help other developers discover this resource.