How can the issue of display errors with 2 or 3-byte characters be resolved in PHP when using MySQL?

When dealing with display errors for 2 or 3-byte characters in PHP and MySQL, the issue can be resolved by ensuring that the character set and collation are properly set for both the database connection and the HTML output. This can be achieved by setting the character set to 'utf8mb4' for both the MySQL connection and the HTML output.

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

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