What are best practices for ensuring proper character encoding in PHP and MySQL interactions?

Proper character encoding in PHP and MySQL interactions is crucial to avoid issues with special characters and ensure data integrity. To ensure proper character encoding, you should set the character set to UTF-8 for both PHP and MySQL connections.

// Set character encoding for PHP
mysqli_set_charset($connection, 'utf8');

// Set character encoding for MySQL
$connection->query("SET NAMES 'utf8'");