PHP echo "\n" prints no line break on a web page
echo "Line one\nLine two"; shows 'Line one Line two' on one line in the browserShort answer
PHP does write the newline character. A browser reads the output as HTML, and HTML treats a newline like any other space. Use nl2br() or a br tag, or add CSS white-space: pre-line. Two other causes: a single-quoted string never turns \n into a newline, and plain-text output needs a Content-Type: text/plain header.
Fix it fast
1Find out which cause you have
Request the script with curl, which shows the raw bytes and does not render HTML. If the line breaks are there in curl but not in the browser, the browser is collapsing them. If curl prints a backslash and an n, the string never contained a newline.
curl -s http://localhost/test.php2Match what you see, then jump to its fix
- 1
curl shows separate lines, while the browser shows one lineThe browser treats the newline as a space, because the output is HTMLFix - 2
curl prints a backslash followed by the letter nThe string is in single quotes, so \n is just two charactersFix - 3
The output is a log, CSV or API text, the newlines are right in curl, and the browser still joins the linesThe response is plain text, but it is sent as HTMLFix
- 1
What does the error look like?
echo "Line one\nLine two"; shows 'Line one Line two' on one line in the browserThe page shows a literal \n between the wordsA plain-text or CSV response runs together on one line in the browser
Cause 1 of 3
The browser treats the newline as a space, because the output is HTML
1Check
A web page is HTML by default. The MDN reference for white-space says that in the normal mode, newline characters in the source are handled like other white space, and sequences of white space are collapsed.
curl -s http://localhost/test.php | head
curl -sI http://localhost/test.php | grep -i content-type2Fix
Turn the newlines into HTML line breaks with nl2br(), which inserts a br tag before each newline, or write br tags yourself. Or keep the newlines and let CSS honour them with white-space: pre-line.
<?php
echo nl2br("Line one\nLine two");
// or, in the page's CSS
// .note { white-space: pre-line; }3Verify
Reload the page. Each line starts on its own line.
curl -s http://localhost/test.phpConfirmed by php.net
Cause 2 of 3
The string is in single quotes, so \n is just two characters
1Check
PHP only turns \n into a line feed inside double-quoted strings. In single quotes, a backslash is literal except in front of another backslash or a single quote.
grep -rnF '\n' . --include=*.php2Fix
Use double quotes for any string that contains an escape such as \n.
<?php
echo 'Line one\nLine two'; // prints the characters \n
echo "Line one\nLine two"; // prints a real newline3Verify
Request the script again with curl and check that the text is on two lines.
curl -s http://localhost/test.phpConfirmed by php.net
Cause 3 of 3
The response is plain text, but it is sent as HTML
1Check
If the response says text/html, the browser applies the HTML rules from the first cause. A plain-text export has to say that it is plain text, and the header must be sent before any output, even a blank line before the opening PHP tag.
curl -sI http://localhost/export.php | grep -i content-type2Fix
Send a text/plain header as the first thing the script does.
<?php
header('Content-Type: text/plain; charset=utf-8');
echo "id,name\n1,Ada\n2,Grace\n";3Verify
The browser shows the lines as written, and curl reports the new content type.
curl -sI http://localhost/export.php | grep -i content-typeConfirmed by php.net
How do I stop it coming back?
- Decide per output whether it is HTML, plain text or JSON, and send the matching Content-Type header on purpose.
- Use double quotes whenever a string holds an escape sequence, and single quotes for strings that should stay literal.
- Use curl, not the browser, to look at the real bytes when output looks wrong.
- Keep PHP files free of text or blank lines before the opening tag, so headers can still be sent.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Line breaks disappear or show as text in a PHP mail() body
New lines (\r\n) are not working in email body
Three things break line breaks in mail(): single-quoted strings, which do not turn \n into a newline; HTML mail, where a plain newline is only whitespace so you need <br> or nl2br(); and the line ending itself, since the manual asks for CRLF between lines.
3 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix