How can developers ensure the validity of email addresses in PHP scripts to prevent errors like "Invalid address" when sending emails?

To ensure the validity of email addresses in PHP scripts, developers can use the built-in filter_var function with the FILTER_VALIDATE_EMAIL filter. This function will check if the email address is in a valid format before sending the email, preventing errors like "Invalid address."

$email = "example@example.com";

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // Send email
    echo "Email sent successfully";
} else {
    echo "Invalid email address";
}