PHP: Email validation

This entry is part 51 of 54 in the series PHP Tutorial

A lot of the times, we would need to ensure users have entered the right format of email address.

We can use a function filter_var() provided by PHP to validate emails.

$email_a = '';
$email_b = 'bogus';

if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
    echo "Your Email is " . $email_a;
    echo "<br><br>";

if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) {
    echo "This ($email_b) email address is considered valid.";
} else
    echo "This ($email_b) email address is considered invalid.";

You can try the above code here.

Series Navigation<< PHP: Creating a dropdown list using PHP
PHP: Form email validation >>

Leave a comment

Leave a Reply