What are common issues with using htmlspecialchars() in PHP, particularly with strings containing special characters like umlauts?

When using htmlspecialchars() in PHP, special characters like umlauts can be improperly encoded, leading to display issues or security vulnerabilities. To solve this, you can specify the character encoding parameter as UTF-8 to ensure proper encoding of special characters.

$string = "Möglichkeiten & Lösungen";
$encoded_string = htmlspecialchars($string, ENT_QUOTES, 'UTF-8');
echo $encoded_string;