\n problem

Can somebody explain to me why php doesn't replace \n with a linefeed?  When I print text that contains \n, the whole string appears on the same line and all the \n are visible. Why?
huxunAsked:
Who is Participating?
 
Richard QuadlingConnect With a Mentor Senior Software DeveloperCommented:
The answer here is if you want breaks in both the code and in the rendered output, you need ...


nl2br("\n");



Regards,

Richard.
0
 
huxunAuthor Commented:
Multiple post ... please delete!
0
 
Richard QuadlingSenior Software DeveloperCommented:
Something to be aware of (you may already know), if you want to show html on several lines and you are using \n to terminate lines, you can use the nl2br function to put in <br> instead of \n.

Regards,

Richard Quadling.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
egomasterCommented:
I recall having a similar problem that is having \n being printed instead of being line feeds.
Can you please post your PHP code and I might remember what was wrong.
0
 
huxunAuthor Commented:
I have already solved this problem with help from some experts on another thread.  As I explained, this is a multiple post, due to a refresh. I really think that bug should be fixed.
0
 
Richard QuadlingSenior Software DeveloperCommented:
<html>
<body>
<?php

echo 'This is on line 1.\nThis is on line 2.\n';

echo "This is on line 3.\nThis is on line 4.\n";

echo nl2br('This is on line 5.\nThis is on line 6.\n');

echo nl2br("This is on line 7.\nThis is on line 8.\n");

?>
</body>
</html>

produces (via View Source)

<html>
<body>
This is on line 1.\nThis is on line 2.\nThis is on line 3.
This is on line 4.
This is on line 5.\nThis is on line 6.\nThis is on line 7.<br />
This is on line 8.<br />
</body>
</html>

which renders to ...

This is on line 1.\nThis is on line 2.\nThis is on line 3. This is on line 4. This is on line 5.\nThis is on line 6.\nThis is on line 7.
This is on line 8.

Only line 8 is actually on a seperate line.

Regards,

Richard.
0
 
huxunAuthor Commented:
WTF!!!
0
 
huxunAuthor Commented:
I'll give you the points, you deserve them. Don't spend them all at once.
0
 
Richard QuadlingSenior Software DeveloperCommented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.