Jump to content

When to use \n vs <br/> ?


Go to solution Solved by Irate,

Recommended Posts

Hi Guys,

 

I'm new here and just started learning PHP a couple of days ago. I have to say it's not nearly as intense as I thought it would be (coming from the simple html/css world)  :happy-04:

 

Anyways, I'm at the point where i'm learning about the /n line break. I was testing it in dreamweaver and saw that none of the browsers (Firefox, Safari, Chrome, etc.) recognize it, which makes perfect sense since browsers don't understand the PHP language. So I was wondering, when and where is the \n line break useful? I know you can spot the break if you check the source code from the browser, but other than that, there is no change on the front-end.

 

Thanks guys!

Edited by DomMarx
Link to comment
https://forums.phpfreaks.com/topic/279074-when-to-use-n-vs/
Share on other sites

  • Solution

\n is used for plaintext.

Since PHP again does not support <br> and \n isn't written directly into the browser (even though <br>s are recognized by JavaScript as \n), you can use string nl2br( string $string ) to give out a proper HTML-formated string.

Link to comment
https://forums.phpfreaks.com/topic/279074-when-to-use-n-vs/#findComment-1435554
Share on other sites

\n Newlines will only ever "appear" in the source code.  The same goes for \r RETURN;

 

the
tag is for HTML formatting and thus only functions in a browser.  If you check the source code on something you are echoing
, then you will clearly see it.

Link to comment
https://forums.phpfreaks.com/topic/279074-when-to-use-n-vs/#findComment-1435558
Share on other sites

So you don't really need \n then? Unless it's for cleaning up the source code?

Exactly.  But they also come in handy for writing source code for things such as email headers. By putting \r\n after each mail header you allow sendmail to distinguish the characteristics of that message and display them accordingly.

Link to comment
https://forums.phpfreaks.com/topic/279074-when-to-use-n-vs/#findComment-1435603
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.