Example 1 — Validate an Email Address
Check two values with FILTER_VALIDATE_EMAIL. A valid email is returned as is, and an invalid one gives false.
<?php
$good = filter_var('[email protected]', FILTER_VALIDATE_EMAIL);
$bad = filter_var('user@@example', FILTER_VALIDATE_EMAIL);
echo "Good: ", $good !== false ? $good : "invalid", PHP_EOL;
echo "Bad: ", $bad !== false ? $bad : "invalid", PHP_EOL; How It Works
- filter_var() receives the text and FILTER_VALIDATE_EMAIL as the filter.
- The first email has a normal format, so the same string is returned.
- The second one has two @ signs, so the filter returns false.
- We compare with !== false, then print either the email or the word invalid.

