Are there any best practices for handling HTML content in PHP scripts?

When handling HTML content in PHP scripts, it is important to properly escape the content to prevent XSS attacks and ensure that the HTML is displayed correctly. One common approach is to use the htmlspecialchars function to escape the HTML content before outputting it to the browser.

$html_content = "<h1>Hello, World!</h1>";
$escaped_content = htmlspecialchars($html_content);
echo $escaped_content;