The easiest way to validate email addresses in PHP

Here's the absolute easiest way you can validate an email address using PHP. This tiny function takes advantage of the filter_var() function in PHP.

function valid_email($email) {
    return !!filter_var($email, FILTER_VALIDATE_EMAIL);

That said, you really don't even need the function wrapper if you can remember filter_var() and the appropriate constant. Just remember that the return value will be the filtered string or false—not always a boolean value like the function above.

To use it:

if( valid_email('') ) {
    echo 'Valid!';
} else {
    echo 'Not valid!';

Finally, no more crazy regular expressions in your code!

Author avatar

About the author

Creator of Surreal CMS and other web things. Follow me for tweets about JavaScript, CSS, and web programming.