What is the difference between "\n" and "\r\n" in PHP when writing to a file?
When writing to a file in PHP, "\n" represents a newline character, while "\r\n" represents a carriage return followed by a newline character. The difference between the two is in how they are interpreted by different operating systems. "\n" is commonly used in Unix-based systems, while "\r\n" is used in Windows-based systems. To ensure cross-platform compatibility when writing to a file, it's best to use "\r\n" to represent a new line.
// Open a file for writing with carriage return and newline characters
$file = fopen("example.txt", "w");
fwrite($file, "Line 1\r\n");
fwrite($file, "Line 2\r\n");
fclose($file);