New line ("\ n") in PHP does not work

For some strange reason, inserting echo "\n"; and other characters of the Scape sequence do not work for me, so I just use <br /> .

Images of the results of examples in books and other documents look just fine. I am currently using XAMPP and have already used WAMPP with the same actual result. Why is this?

Edit:

I don't seem to understand the concept after comparing your answers with this: PHP Linefeeds (\ n) is not working

Edit:

Sorry, I didn’t understand that the link above refers to writing PHP code to a file. I just wonder why I have some examples of PHP examples that use \ n, even if they are displayed on a web page. Thanks to everyone.

+8
php xampp
source share
6 answers

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

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

This will display 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 refers to sending output to a file, not to the browser.

+19
source share

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

+3
source share

<br /> is the HTML tag for the new line, while "\ n" displays the new line (for the real one).

The browser does not print a new line each time the HTML file moves to the next line.

+2
source share

echo "\n" probably works, just not the way you expect.

This command will add a new line character. Of its sounds, you use a browser to view the results. Please note: if you wrote an HTML file with the contents of the body, which looked like this:

 <p>This is a test </p> 

In the browser, the rendering will not include new lines and instead simply displays “This is a test”

If you want to see new lines, you can view the source and you will see that the source code contains new lines.

The rule of thumb is that if you need new lines in the browser, you need to use HTML (for example, <br /> ), and if you want it in text format, you can use \n

+2
source share

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

As already mentioned, HTML does not display \n as a new line. It only recognizes the tag <br> html

+1
source share

If you are working with HTML (for example, viewing the result in a browser), you should use the HTML path for the lines, which: <br>

0
source share

All Articles