What are the potential pitfalls of using different character encodings in PHP when writing to text files?

Using different character encodings in PHP when writing to text files can lead to issues such as garbled or incorrectly displayed text. To avoid this problem, it's important to ensure that the character encoding used in PHP matches the encoding of the text being written to the file.

// Set the character encoding to UTF-8 before writing to the file
header('Content-Type: text/html; charset=utf-8');
$filename = 'example.txt';
$text = 'Hello, 你好, مرحبا';
file_put_contents($filename, $text, LOCK_EX);