How can UTF-8 encoding affect the output of PHP functions like htmlentities()?

When using the htmlentities() function in PHP, it is important to ensure that the input string is properly encoded in UTF-8 to handle special characters correctly. If the input string is not encoded in UTF-8, the htmlentities() function may not be able to properly encode special characters, leading to unexpected output or security vulnerabilities. To fix this issue, you can use the mb_convert_encoding() function to convert the input string to UTF-8 before passing it to htmlentities(). This will ensure that special characters are properly encoded and displayed in the output.

$input = "Special characters like é and ü";
$input_utf8 = mb_convert_encoding($input, 'UTF-8');
$output = htmlentities($input_utf8);
echo $output;