Page 1 of 1

Use of \n

Posted: Sun Apr 30, 2006 2:29 pm
by Iron_Druid
Hello programmers, this is my 1st post around here,

I am newbie in php stuff and I have a query that you might solve it to me. Basically, I don't understand the purpose of \n on CGI Website use of php.

For exaple

Code: Select all

echo "Hello\nWorld";
The PHP Output is:
Hello
World
Ok, I can see the the difference.



But... the same code ->

WebPage Output
Hello World
There is no difference because in order to achieve the look of the above PHP Output I should use the <br> HTML tag.

My question simple: If i use PHP only for Websites (not for Command Line Interface - CLI) why should I use the \n ???

Regards,
Druid

Posted: Sun Apr 30, 2006 2:35 pm
by Benjamin
If you have loops that display data on a web page, and you don't use the \n to create line feeds, then you view the source code, everything in the loop might be on one line.

Posted: Sun Apr 30, 2006 2:37 pm
by hawleyjr
Well, <br> (Which should be <br /> ) takes up twice the space of \n in a database :)

Check out: nl2br()

Posted: Sun Apr 30, 2006 2:42 pm
by d3ad1ysp0rk
hawleyjr wrote:Well, <br> (Which should be <br /> ) takes up twice the space of \n in a database :)

Check out: nl2br()
Haha, although that is a good reason, I usually keep things in \n form due to the fact that the data might not always be displayed on a webpage.
They might want to download it in CSV format. They might want to edit it (meaning you need to convert the <br />'s back to \n's). Etc etc.

Posted: Sun Apr 30, 2006 2:43 pm
by hawleyjr
d3ad1ysp0rk wrote:
hawleyjr wrote:Well, <br> (Which should be <br /> ) takes up twice the space of \n in a database :)

Check out: nl2br()
Haha, although that is a good reason, I usually keep things in \n form due to the fact that the data might not always be displayed on a webpage.
They might want to download it in CSV format. They might want to edit it (meaning you need to convert the <br />'s back to \n's). Etc etc.
lol, I know me too. just felt like we needed some Sunday Humor :)

Posted: Sun Apr 30, 2006 3:28 pm
by Chris Corbyn
hawleyjr wrote:Well, <br> (Which should be <br /> ) takes up twice the space of \n in a database :)

Check out: nl2br()
<br /> is not valid HTML.... since self-closing tags don't exist. This is only true of you're using an XHTML doctype.

Yeah, the main reason for using \n would be to make the output source look pleasant and also in textareas or <pre> tags.

Posted: Sun Apr 30, 2006 3:43 pm
by Iron_Druid
So the use of \n is a matter of the HTML source code appearance. Aha, I see the difference now.

But I did not understand the difference between <br> and < /br> ... :?: For example if I convert all my \n to <br /> is there any problem with HTML W3C Validator or somethin ?

Posted: Sun Apr 30, 2006 5:13 pm
by R4000
<br> = html standard.
<br /> = xhtml standard.

Its recommended you use xhtml style tags, (dont ask me why, but everyone flames me when i dont xD)

\

Posted: Mon May 01, 2006 1:48 am
by dibyendrah
Usually \n is required in most case in sending email, saving file in csv etc. <BR> is to generate the line feed in browser only and not for other purpose .

Posted: Mon May 01, 2006 8:14 am
by Iron_Druid
Thank you very much for your replies. :D