What are the best practices for handling character encoding issues in PHP, especially when working with external systems like webshops?

Character encoding issues can arise when working with external systems like webshops due to differences in how characters are encoded and displayed. To handle this in PHP, it is important to ensure that all input and output data is properly encoded and decoded using functions like utf8_encode() and utf8_decode(). Additionally, setting the appropriate charset in the HTML meta tag can help ensure that characters are displayed correctly on the webpage.

// Set the charset in the HTML meta tag
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

// Encode input data before sending to external system
$input_data = "Some text to encode";
$encoded_data = utf8_encode($input_data);

// Decode output data received from external system
$output_data = "Some text to decode";
$decoded_data = utf8_decode($output_data);