Is using htmlspecialchars function recommended for displaying HTML content in PHP text windows?

When displaying HTML content in PHP text windows, it is recommended to use the htmlspecialchars function to prevent any potential security vulnerabilities such as cross-site scripting (XSS) attacks. This function will convert special characters in the HTML content to their respective HTML entities, ensuring that the content is displayed as plain text rather than being interpreted as HTML code.

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