How can PHP be used to write text to a file while preserving line breaks?

When writing text to a file using PHP, line breaks may not be preserved by default. To ensure that line breaks are maintained, you can use the PHP_EOL constant to represent the appropriate line break character for the current platform (e.g., "\n" for Unix-based systems). By appending PHP_EOL to each line of text that you write to the file, you can ensure that line breaks are preserved.

<?php
// Open a file for writing
$file = fopen("output.txt", "w");

// Text content with line breaks
$text = "Line 1" . PHP_EOL . "Line 2" . PHP_EOL . "Line 3";

// Write text to the file while preserving line breaks
fwrite($file, $text);

// Close the file
fclose($file);
?>