Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    May 2008
    Posts
    446
    Thanks
    23
    Thanked 5 Times in 5 Posts

    need to create new line break with \n

    I am working on some hw and I need to create a new line using \n. I have putting it in all different ways with an echo, but the best I get is make \n not be visible.

    echo $var."\n";

    I will put this in a foreach loop, but all the output stays on the same line. I have read it will only work when put inside html elements, so I had the list get created inside <p></p> tags... no difference.

    I now understand why I always use <br /> for new lines... '\n' is very fickle.

    I appreciate the help

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    \n itself isn't fickle, but it is pretty specific in it's uses. If you're actually wanting new lines in you viewable text (what the user sees when looking at your script/text) then your "\n" will work. In terms of html, it wont always work. In my experience, if you're wanting new lines between tags in your html source, putting a "\n" inbetween tags will do that. If you're wanting breaks in your html text, either use <p> tags or <br />.

    I don't really know the specifics, but I know that your browser wont treat the \n as expected in some scenarios, so it's best to stick by my rule imo - break in source, \n; break in text, paragraphing or <br />.

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,327
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    As BP says, \n (inside double quotes) works for text based content (EG text files) but it doesn't work in html.

    html is exactly what its name suggests - markup language. Markup means tags to describe things. That means that any breaks are marked up using the <br> tag. That is how html works and that is why your code does not work.

    Normal text is not a markup language.

    Html is a web page. The html source is supposed to be a set of mark up INSTRUCTIONS on how to display the content. Therefore line / carriage returns are ignored.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •