Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP echo "\n" prints no line break on a web page

Errorecho "Line one\nLine two"; shows 'Line one Line two' on one line in the browser

Short 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.

  • Applies to PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.php
  2. 2Match what you see, then jump to its fix

    1. 1curl shows separate lines, while the browser shows one lineThe browser treats the newline as a space, because the output is HTMLFix
    2. 2curl prints a backslash followed by the letter nThe string is in single quotes, so \n is just two charactersFix
    3. 3The 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

What does the error look like?

  • echo "Line one\nLine two"; shows 'Line one Line two' on one line in the browser
  • The page shows a literal \n between the words
  • A 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-type

2Fix

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.php

Confirmed by php.net

Did this fix it?

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=*.php

2Fix

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 newline

3Verify

Request the script again with curl and check that the text is on two lines.

curl -s http://localhost/test.php

Confirmed by php.net

Did this fix it?

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-type

2Fix

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-type

Confirmed by php.net

Did this fix it?

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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.