What are some common pitfalls when working with UTF-8 characters in PHP and MySQL?

Common pitfalls when working with UTF-8 characters in PHP and MySQL include not setting the character encoding properly, which can result in garbled or incorrect data being stored or displayed. To solve this issue, ensure that both PHP and MySQL are set to use UTF-8 encoding throughout the entire process, from connecting to the database to displaying the data.

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

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