How does using XHTML affect character encoding in PHP websites and what additional steps need to be taken?

When using XHTML in PHP websites, it is important to specify the character encoding in the document header to ensure proper display of special characters. This can be done by adding a meta tag with the charset attribute set to the desired encoding (e.g., UTF-8). Additionally, it is recommended to encode any dynamic content using PHP functions like htmlspecialchars to prevent XSS attacks.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<?php
// Example of encoding dynamic content
$unsafe_input = "<script>alert('XSS attack');</script>";
$safe_output = htmlspecialchars($unsafe_input, ENT_QUOTES, 'UTF-8');
echo $safe_output;
?>
</body>
</html>