How can PHP developers ensure that entities like "&" are not only used for HTML but also for other XML derivatives?

When working with XML derivatives, PHP developers can ensure that entities like "&" are properly encoded by using the htmlspecialchars() function with the ENT_XML1 flag. This will ensure that special characters are encoded correctly for XML.

$xmlString = '<example>&</example>';
$encodedXml = htmlspecialchars($xmlString, ENT_XML1);
echo $encodedXml;