Is it necessary to escape characters like <&>" in UTF-8 encoded XHTML documents in PHP?

In UTF-8 encoded XHTML documents in PHP, it is necessary to escape characters like <&>" to ensure proper rendering and prevent potential security vulnerabilities such as XSS attacks. This can be done using PHP's htmlspecialchars() function, which converts special characters to their HTML entity equivalents.

$text = &quot;&lt;p&gt;This is a sample text with special characters like &lt;&amp;&gt;\&quot;&lt;/p&gt;&quot;;
$escaped_text = htmlspecialchars($text, ENT_QUOTES, &#039;UTF-8&#039;);
echo $escaped_text;