'Echo won't display new lines in PHP [duplicate]

I'm trying to display some text on my website from my database.

I've tried using echo $pri->maintext - it works but it does not include blank lines and new lines.

Value of maintext in database:

This is line 1
This is line 2

This is line 4

but it displays:

This is line 1This is line 2This is line 4

How can I can solve this? I need the text to display new lines and blank lines.



Solution 1:[1]

The text is displayed with newlines, but HTML doesn't render newline characters. A line break in HTML is a <br/> element.

You can replace linebreaks with <br/> elements with the nl2br() function:

echo nl2br($your_string_value);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David