Write \n inside a double-quoted string to print a new line: echo "Line 1\nLine 2";. The constant PHP_EOL does the same and reads more clearly at the end of a line: echo 'Done' . PHP_EOL;. In a web page, a newline does not show up as a line break; there you need <br> or nl2br().
Single quotes vs double quotes
Escape sequences such as \n and \t only work in double quotes. In single quotes, '\n' is two characters: a backslash and the letter n. This is the usual reason "\n does not work".
The strings page lists every escape sequence double quotes understand.
PHP_EOL vs "\n"
PHP_EOL is the line ending of the system PHP is running on: "\n" on Linux and macOS, "\r\n" on Windows. Use it for terminal output and files you will read on the same machine. When a format defines its own line ending, use that instead: HTTP headers and email need "\r\n", and most tools read "\n" fine everywhere.
"\r" is a carriage return, "\n" a line feed. Windows files end lines with both, "\r\n", which is why text from a Windows user or a <textarea> often contains \r characters you did not expect.
New line in HTML: br and nl2br
A browser collapses newlines into a single space, so echo "a\nb" shows a b on a page. Either print <br> yourself, or keep the newlines in your text and convert them with nl2br(), which inserts <br /> before each one. Run this and compare the Output and Page tabs:
On the Output tab all three look similar. On the Page tab the first one runs together on one line, nl2br() breaks it, and <pre> keeps the newlines as written.
Show textarea input with its line breaks
Text from a <textarea> keeps the lines the user typed, but you must escape it before printing it into HTML. The order matters: htmlspecialchars() first, then nl2br(), otherwise the <br /> tags themselves would be escaped. Run it, type a few lines into the box and press Preview: the same script runs again with $_POST filled in.
A textarea line break can arrive as "\r\n" or "\n"; both contain "\n", so counting that character works either way. htmlspecialchars() explains why escaping is not optional here.
Split a string by newline
explode("\n", $text) works for text you created yourself. For text from users or files, the line endings may be "\r\n", "\n" or "\r"; preg_split('/\R/', $text) handles all of them, because \R matches any line break.
Remove line breaks from a string
To turn multi-line text into one line, replace every kind of line ending. Replace "\r\n" before the single characters, or each Windows line break would become two spaces:
trim() and rtrim() remove newlines at the ends of a string but never in the middle, so they are the right tool for a line read from a file and the wrong one for joining lines.
Frequently Asked Questions
How do I echo a new line in PHP?
Put \n inside a double-quoted string: echo "Line 1\nLine 2";. Or append the constant: echo 'Line 1' . PHP_EOL;. In a browser, a newline is not shown; use <br> or nl2br() there.
Why does \n not work in PHP?
Either the string is in single quotes, where '\n' is a backslash and an n, or the output is shown as HTML, where the browser treats a newline like a space. Use "\n" in double quotes for text output and nl2br() or <br> for web pages.
What is PHP_EOL?
A constant holding the line ending of the operating system PHP runs on: "\n" on Linux and macOS, "\r\n" on Windows. Use it for output meant for the terminal or local files; for HTTP, emails and CSV files the format usually fixes the line ending regardless of the OS.
What does nl2br do in PHP?
nl2br($text) inserts <br /> before every line break in the string, so text typed in a <textarea> keeps its lines when shown in HTML. Escape first: nl2br(htmlspecialchars($text)).
How do I remove line breaks from a string in PHP?
Replace all three kinds of line ending: str_replace(["\r\n", "\r", "\n"], ' ', $text), or with a regex, preg_replace('/\R/', ' ', $text). To remove only the trailing newline, use rtrim($text, "\r\n").