What are the best practices for setting the character encoding in PHP scripts and MySQL databases?

When working with PHP scripts and MySQL databases, it is important to ensure that the character encoding is consistent to avoid issues with displaying special characters or text encoding errors. The best practice is to set the character encoding to UTF-8 for both PHP scripts and MySQL databases. This can be done by specifying the encoding in the PHP script using the header function and setting the character set in the MySQL connection.

// Set character encoding in PHP script
header('Content-Type: text/html; charset=utf-8');

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