What are the best practices for handling character encoding in PHP when dealing with data from different sources?

Character encoding issues can arise when dealing with data from different sources in PHP. To handle this, it's important to ensure that the data is consistently converted to a single character encoding, such as UTF-8. One way to do this is by using functions like mb_convert_encoding() or iconv() to convert the data to the desired encoding.

// Example of converting data to UTF-8 encoding
$data = "Some data with unknown encoding";
$encoding = mb_detect_encoding($data, 'auto');
$data_utf8 = mb_convert_encoding($data, 'UTF-8', $encoding);

echo $data_utf8;