What are best practices for validating email addresses in PHP forms?

Validating email addresses in PHP forms is important to ensure that the data entered is in the correct format. One common way to validate email addresses is by using PHP's built-in filter_var function with the FILTER_VALIDATE_EMAIL flag. This function checks if the email address is in a valid format, such as user@example.com.

$email = $_POST['email'];

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // Invalid email address
    echo "Invalid email address";
} else {
    // Valid email address
    echo "Email address is valid";
}