23

For some strange reason, inserting echo "\n"; and other scape sequence characters are not working for me, that's why I am just using <br /> instead.

The images of the results of examples in books and other documentations seems just alright. I'm currently using XAMPP and already used WAMPP with the same actual result. Why is that?

Edit:

It seems that I cannot understand the concept after comparing your answers with this: PHP Linefeeds (\n) Not Working

Edit:

Sorry I didn't realized that the link above is referring to a php code writing to a file. I just wonder why I have these few php sample programs that uses \n even though it outputs in a webpage. Thanks everyone.

Community
  • 1
  • 1
Arman
  • 1,434
  • 2
  • 22
  • 42
  • 12
    Because you're outputting HTML, and in HTML, "\n" is treated as whitespace? – andrewsi Sep 13 '12 at 16:25
  • I want to emphasize that I am using double quotes and they are embedded in php code. Also the file extension I used is .php. – Arman Sep 13 '12 at 16:25
  • 1
    In HTML, a newline is only treated as such in a context where `white-space: pre` or similar (default style/behavior of the `pre` element). In other HTML contexts, a newline is treated as white space. – ACJ Sep 13 '12 at 16:28
  • 1
    PHP outputs to a browser (usually) which renders the output as HTML by default. Using `header('Content-type: text/plain');` will cause the output to be rendered as plain text. – Herbert Sep 13 '12 at 16:31

9 Answers9

50

When you run a PHP script in a browser, it will be rendered as HTML by default. If the books you’re using show otherwise, then either the code or the illustration is inaccurate. You can use “view source” to view what was sent to the browser and you’ll see that your line feeds are present.

<?php
echo "Line 1\nLine 2";
?>

This will render in your browser as:

Line 1 Line 2

If you need to send plain text to your browser, you can use something like:

<?php
header('Content-type: text/plain');
echo "Line 1\nLine 2";
?>

This will output:

Line 1
Line 2

PHP Linefeeds (\n) Not Working is referring to sending output to a file rather than the browser.

Community
  • 1
  • 1
Herbert
  • 5,698
  • 2
  • 26
  • 34
7

You should be looking for nl2br(). This will add line breaks (<br>) to your output which will be rendered by the browser; newlines are not.

Bojangles
  • 99,427
  • 50
  • 170
  • 208
5

The echo "\n" is probably working, just not the way you expect it to.

That command will insert a new line character. From the sounds of it, you're using a browser to view your output. Note that if you wrote an HTML file that had a body contents that looked like:

<p>This
is
a
test </p>

The browser rendering would not include the new lines, and would instead just show "This is a test"

If you want to see the newlines, you could view source, and you'll see that the source code includes the new lines.

The rule of thumb is that if you need new lines in a browser, you need to use HTML (e.g. <br />), while if you want it in plain text, you can use the \n

ernie
  • 6,356
  • 23
  • 28
2

<br /> is the HTML Tag for new line, whereas "\n" is to output a new line (for real).

The browser doesn't output a new line each time the HTML file goes to the next line.

Bgi
  • 2,513
  • 13
  • 12
1

You can use the nl2br function to convert \n to <br>

As said before, HTML does not render \n as new line. It only recognizes the <br> html tag

Stano
  • 8,749
  • 6
  • 30
  • 44
Hernan Velasquez
  • 2,770
  • 14
  • 21
1

If you are working with HTML (viewing the result in browser for example) you have to use the HTML way of linebreaks which is: <br>

Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203
0

/n only works if it is used as a simple text but here as we code in a html doc it takes it as a HTML text hence you can use </br> tag instead.

Kunal Raut
  • 2,495
  • 2
  • 9
  • 25
0

PHP outputs on the browser and browser only render output in HTML, any other output format will be ignored by the browser.

If you want browser to keep your standard output format as it is, you should enclose your output under HTML's <pre> tag. It preserves the formatting:

echo "<pre>";
echo "This is first line\nThis is new line";
echo "</pre>";

This will be rendered as

This is first line
This is new line

Alternatively, you can mention content type to be plain text in the header:

header('Content-type: text/plain');
echo "This is first line\nThis is new line";

This will tell the browser to render the output as plain text. And the browser will encolse the output automatically in <pre> tag.

Rakesh
  • 1
-1

solution is echo nl2br or=> <br>