Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New line ("\n") in PHP is not working

Tags:

php

xampp

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.

like image 430
Arman Avatar asked Sep 13 '12 16:09

Arman


People also ask

Does \n work in PHP?

Answer: Use the Newline Characters ' \n ' or ' \r\n ' You can use the PHP newline characters \n or \r\n to create a new line inside the source code.

What is the \n in PHP?

\n is the newline or linefeed, other side \r is the carriage return. They differ in what uses them. Windows uses \r\n to signify the enter key was pressed, while Linux and Unix use \n to signify that the enter key was pressed.

Does \n make a new line?

The \n Character The other way to break a line in C++ is to use the newline character — that ' \n ' mentioned earlier. This is line one. This is line two.

What does br /> mean in PHP?

<br /> is a HTML line-break, whereas \n is a newline character in the source code. In other words, <br /> will make a new line when you view the page as rendered HTML, whereas \n will make a new line when you view the source code.


4 Answers

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.

like image 125
Herbert Avatar answered Oct 16 '22 00:10

Herbert


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.

like image 44
Bojangles Avatar answered Oct 16 '22 00:10

Bojangles


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

like image 31
ernie Avatar answered Oct 16 '22 01:10

ernie


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

like image 24
Bgi Avatar answered Oct 16 '22 00:10

Bgi