What are the potential pitfalls of double UTF-8 encoding and how can they be avoided in PHP scripts?

Double UTF-8 encoding can lead to character corruption and display issues. To avoid this problem in PHP scripts, it is essential to ensure that the input data is properly decoded before encoding it again.

// Example PHP code snippet to prevent double UTF-8 encoding

// Assuming $input contains the double UTF-8 encoded string
$decoded_input = utf8_decode($input);
$encoded_output = utf8_encode($decoded_input);

// Use $encoded_output in your application to avoid double UTF-8 encoding issues