Use of \n

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Iron_Druid
Forum Newbie
Posts: 3
Joined: Sun Apr 30, 2006 2:18 pm
Location: Greece, Thessaloniki

Use of \n

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Well, <br> (Which should be <br /> ) takes up twice the space of \n in a database :)

Check out: nl2br()
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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 :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
User avatar
Iron_Druid
Forum Newbie
Posts: 3
Joined: Sun Apr 30, 2006 2:18 pm
Location: Greece, Thessaloniki

Post 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 ?
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post 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)
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

\

Post 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 .
User avatar
Iron_Druid
Forum Newbie
Posts: 3
Joined: Sun Apr 30, 2006 2:18 pm
Location: Greece, Thessaloniki

Post by Iron_Druid »

Thank you very much for your replies. :D
Post Reply