PHP Echo Line Breaks
When writing output in PHP, specifically when dealing with text logs or HTML rendering, the choice of line break character often feels trivial until a script stops working on one specific server but runs perfectly elsewhere. The core issue lies in the history of operating systems and how they define where a paragraph ends.
The standard Unix/Linux newline is \n (LF, Line Feed). This was established early on because it required only a single byte to terminate a line, making storage slightly more efficient than older Macintosh standards which used \r (CR, Carriage Return) alone. Windows, however, adopted a different approach for compatibility and legacy reasons, combining both into \r\n (CRLF).
In a Linux environment, if you echo \r without an accompanying \n, the cursor moves to the beginning of the current line but does not advance to the next one. Visually, this can overwrite previous text or create strange overwriting effects in terminals that don't handle raw carriage returns gracefully. Conversely, on Windows, echoing just \n will technically work because modern Windows terminal emulators and web browsers are designed to interpret a lone LF as a line break for compatibility. However, strictly speaking, the native OS expectation remains CRLF.
The Cross-Platform Solution
The most robust solution that works regardless of whether the server is running on Ubuntu, macOS, or Windows is PHP's built-in constant PHP_EOL.
Using echo "Line 1" . PHP_EOL . "Line 2"; ensures that your output contains the correct sequence for the underlying operating system. On Linux servers, PHP_EOL expands to \n. On Windows servers, it expands to \r\n. This abstraction layer handles the translation automatically, preventing you from having to write conditional logic based on the server's OS or hardcoding specific byte sequences that might break in unexpected environments.
If you are writing a script to generate a .txt log file intended for reading by humans or parsing tools, consistency is key. If your application runs on a shared hosting environment where the underlying OS varies (rare but possible with containerization), sticking to PHP_EOL guarantees that the line endings match the host system's expectations, avoiding potential issues with text editors like Notepad on Windows failing to render files correctly if they only contain \n.
Handling HTML Output
The requirements change slightly when you are not outputting raw text but rather rendering HTML markup. In this context, PHP_EOL is still useful for generating the structure of your data, but browsers ignore whitespace between tags anyway. If you are constructing a block of HTML strings in PHP, using PHP_EOL within your string concatenation keeps the code readable and ensures that if you later decide to output raw text (like a debug log) mixed with HTML, the line endings remain consistent with the server's OS.
However, be cautious about mixing these approaches when dealing with file downloads or email sending. If you are forcing a download of a .txt file from a browser, the browser will interpret the content based on its own settings. Using PHP_EOL ensures that if a user downloads this file from a Linux server and opens it in a Mac terminal, the line breaks render correctly without manual conversion tools.
A Note on Configuration
While PHP_EOL is generally the best practice, there are edge cases involving configuration files or specific frameworks where you might manually set a default newline character. For example, some older logging libraries allowed you to configure the separator explicitly. If you find yourself in an environment where PHP_EOL behaves unexpectedly (such as inside a Docker container with a non-standard line ending policy), you can define your own constant:
define('APP_EOL', "\n"); // Forces Unix style everywhere
// or
define('APP_EOL', "\r\n"); // Forces Windows style everywhere
However, doing so usually introduces friction if that file is opened on a different machine. Unless you are strictly controlling the entire pipeline and know exactly how every downstream system will parse the bytes, relying on PHP_EOL is the safest path to cross-platform compatibility.