What are common pitfalls when trying to include line breaks in text files using PHP?

Common pitfalls when trying to include line breaks in text files using PHP include using the wrong line break character for the operating system (e.g., using "\n" instead of "\r\n" on Windows), not properly encoding the line breaks for HTML output, and not considering the encoding of the text file. To solve these issues, ensure you use the correct line break character for the operating system, properly encode line breaks for HTML output using nl2br(), and consider the encoding of the text file when reading or writing.

<?php
// Example of including line breaks in a text file
$text = "First line\nSecond line\nThird line";
$encoded_text = nl2br($text); // Encode line breaks for HTML output

$file = fopen("output.txt", "w");
fwrite($file, $encoded_text);
fclose($file);

echo "Text with line breaks written to output.txt";
?>