11 Vote

PHP: Newline \n is not working

Question by Guest | Last update on 2020-11-01 | Created on 2011-12-21

I want to create a line break in PHP. From JavaScript, I know the command \n for that. I am using the following code:

echo "First Line \n Second Line";

However, I can see nothing of this new line in the browser. The output is "First Line Second Line" just  written in one line after each other. How can that be?

ReplyPositiveNegative
22 Votes

At this point, PHP is only creating the source code of the HTML document. When you have look at the source code in the browser, you should see the created line break in the code.

In the browser itself, you can see nothing of it, since only HTML tags such as <br>, <p> and so forth are producing a new line in the browser.

In the source code, the line breaks are only usable for convenience purposes, therefore a \n in PHP code has only the function to make the source code easier to read. Exactly the same effect has a line break created by using the enter key in your source code editor.

In JavaScript, it is a bit different. There is not such a distinction between source code and strings. Here, a \n directly becomes a "visible" line break, for example, used within a dialog box, created by the function alert().
Last update on 2020-11-01 | Created on 2011-12-23

ReplyPositive Negative
Reply

Related Topics

PHP: Sending an E-Mail

Tutorial | 0 Comments

PHP: Upload of large Files

Tutorial | 0 Comments

PHP: Current Date and Time

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.