What are the potential pitfalls of not ensuring that both files and the database are set to UTF-8 when working with PHP?

If files and the database are not set to UTF-8 when working with PHP, it can lead to encoding issues and result in garbled or incorrect data being stored or displayed. To ensure proper handling of UTF-8 characters, both files and the database should be set to UTF-8 encoding.

// Set UTF-8 encoding for PHP files
header('Content-Type: text/html; charset=utf-8');
mb_internal_encoding('UTF-8');

// Set UTF-8 encoding for MySQL database connection
$mysqli = new mysqli('localhost', 'username', 'password', 'database');
$mysqli->set_charset('utf8');