What are best practices for handling special characters like Umlauts in PHP code?

Special characters like Umlauts can sometimes cause issues in PHP code, especially when handling strings. To properly handle these characters, it's important to use the correct character encoding, such as UTF-8, and to sanitize input data to prevent potential security vulnerabilities.

// Set the character encoding to UTF-8
header('Content-Type: text/html; charset=utf-8');

// Sanitize input data using htmlspecialchars
$input = "Möglichkeit";
$sanitized_input = htmlspecialchars($input, ENT_QUOTES, 'UTF-8');

echo $sanitized_input;