What is the purpose of using htmlentities in PHP when handling email addresses?

When handling email addresses in PHP, using htmlentities helps prevent email addresses from being exploited by malicious users for cross-site scripting attacks. By converting special characters in the email address to their HTML entity equivalents, the email address is displayed as plain text in the HTML output, making it harder for attackers to inject harmful code.

$email = "john.doe@example.com";
$encoded_email = htmlentities($email);
echo $encoded_email;