What are some potential pitfalls of using utf8_encode() in PHP?

Using utf8_encode() in PHP can potentially cause issues if the input string is already encoded in UTF-8, as it may result in double encoding and corrupt the data. To avoid this pitfall, it's recommended to check the encoding of the string before applying utf8_encode().

// Check if the input string is not already UTF-8 encoded
if(mb_detect_encoding($inputString, 'UTF-8', true) !== 'UTF-8'){
    $encodedString = utf8_encode($inputString);
} else {
    $encodedString = $inputString;
}

// Use $encodedString for further processing