How can one ensure consistency in character encoding between old and new websites when using PHP and MySQL?

To ensure consistency in character encoding between old and new websites when using PHP and MySQL, it is important to set the character encoding for both the database connection and the HTML output to UTF-8. This can be done by specifying the charset in the connection string for MySQL and setting the content-type header in PHP.

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

// Set character encoding for HTML output
header('Content-Type: text/html; charset=utf-8');