What are the best practices for handling UTF-8 encoding issues in PHP when working with database data?

When working with database data in PHP, it is important to ensure that UTF-8 encoding is handled correctly to avoid issues with special characters or emojis displaying incorrectly. One common solution is to set the connection charset to UTF-8 when connecting to the database, and also ensure that the PHP files themselves are saved with UTF-8 encoding.

// Set the connection charset to UTF-8
$mysqli = new mysqli("localhost", "username", "password", "database");
$mysqli->set_charset("utf8");

// Ensure PHP files are saved with UTF-8 encoding
// Add this line at the beginning of your PHP files
header('Content-Type: text/html; charset=utf-8');