What role does htmlentities() function play in handling HTML code in PHP?
When handling HTML code in PHP, it's important to properly encode special characters to prevent cross-site scripting (XSS) attacks. The htmlentities() function in PHP converts special characters to their HTML entities, making the code safe to output in the browser.
<?php
$unsafe_html = "<script>alert('XSS attack!');</script>";
$safe_html = htmlentities($unsafe_html);
echo $safe_html;
?>