New Line on PHP CLI

New Line on PHP CLI

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

New Line on PHP CLI

The behavior you are encountering stems from a fundamental difference between how text is represented in source code versus how it is interpreted by the operating system's terminal. When you write echo 'this is my text\r\n'; within a PHP script, the string literal contains exactly what you see: the characters r, b, a, and c, followed by n. The sequence \r\n is not automatically translated into line breaks simply because it resembles one.

The issue lies in how different environments handle escape sequences. In a web browser context, PHP's output handler often processes these sequences before sending data to the client, or the browser interprets them correctly due to its specific rendering engine. However, the standard output stream of the command-line interface (CLI) is typically raw. It outputs bytes directly to the terminal without interpreting escape codes unless explicitly told to do so.

To resolve this, you must ensure that PHP treats the string as a literal line break rather than a sequence of characters. The most direct approach is to use an actual newline character instead of the escape sequence. You can achieve this by using the double-quoted syntax for your strings, which allows PHP to interpret backslash sequences, or more robustly, by using PHP_EOL.

Consider the following adjustment:

<?php
echo "this is my text" . PHP_EOL;
echo 'next line';

In this snippet, double quotes allow PHP to parse \n as a newline character if you were to use it there, but switching to PHP_EOL is the superior practice. PHP_EOL is a constant that holds the appropriate line ending for your current operating system. On Windows, this resolves to \r\n, while on Linux and macOS, it uses \n. This ensures cross-platform compatibility without relying on platform-specific hardcoding or guessing which escape sequence your specific terminal expects.

If you must stick with single quotes or need to output a literal backslash followed by 'r' and 'n', the code remains as you wrote it, and that is functionally correct for displaying those specific characters. However, since your goal is line breaks, replacing the string content entirely is the cleanest path forward.

There is also the matter of stream buffering. Sometimes, even after correcting the newline characters, output might appear on a single line if the script terminates without flushing the buffer or if the terminal input is tied to a pipe. While less common in simple scripts, ensuring you are not piping output into a tool that ignores trailing newlines (like head or certain log parsers) is part of the troubleshooting process.

For developers using frameworks like Laravel, this distinction often surfaces when running artisan commands from the CLI versus accessing routes via a browser. The framework handles HTML rendering and line breaks seamlessly for web requests, but CLI tasks require explicit attention to output formatting. If you are building custom CLI tools or migration scripts, always assume the environment is raw until proven otherwise.

Ultimately, the solution replaces the escape sequence with a platform-agnostic constant. By using PHP_EOL, you delegate the responsibility of line ending detection to PHP itself, ensuring your script behaves consistently whether it runs on a local Windows machine, a Linux server, or a macOS laptop.

Our network