SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 27

Thread: new line

  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,777
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    new line

    What is the new line symbol in HTML?

    Is it \n ?

    What does that do again?

    I tried using it in my PHP and HTML but it doesn't work in either?!


    Debbie

  2. #2
    Non-Member
    Join Date
    Dec 2010
    Location
    /home/pc
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Addict bronze trophy AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    298
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    In HTML you can use <br>.

    Writing innerHTML you might write
    var build='<p>First line<\/p>\n<p>2nd line<\/p>\n';

    If you were to look at the string you have built, say using an alert(build); the \n characters show the code in its correct position as follows:
    <p>First line</p>
    <p>2nd line</p>

    Without the \n the string would not break at the end of the </p> and would be one long line.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,832
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    In PHP use nl2br() to convert new lines into the HTML equivalent.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,777
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I'm not getting this.

    Here is my code...

    Code:
    <?php
    	// Get Configuration settings.
    	require('includes/config.inc.php');
    
    	// Set page title.
    	$page_title = 'About';
    
    	// Get Header file.
    	require('includes/header.html');
    ?>
    <h3>About</h3>
    <p>This website is about...</p>
    <p>Apples</p>\n<p>Oranges</p>\n<p>Bananas</p>
    <?php
    	// Get Footer file.
    	include ('includes/footer.html');
    ?>
    If I take out my two \n's then one line in the View Code I get...

    <p>Apples</p><p>Oranges</p><p>Bananas</p>


    If I leave them in, then I still get one line in the View Code...

    <p>Apples</p><p>Oranges</p><p>Bananas</p>

    and in my web page I get...

    Apples\nOranges\nBananas



    Debbie

  6. #6
    SitePoint Addict bronze trophy AllanP's Avatar
    Join Date
    Sep 2010
    Location
    Australia
    Posts
    298
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    In straight HTML the \n is not required as the </p> will automatically break when the code is rendered on the page.
    The code sample you have provided needs to look like this.
    <p>This website is about...</p>
    <p>Apples</p>
    <p>Oranges</p>
    <p>Bananas</p>

  7. #7
    Non-Member
    Join Date
    Dec 2010
    Location
    /home/pc
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not getting this.
    <p>s are block-level elements and so don't need a new line between them. If you want to display something that normally appears inline in html on a new line then you can use <br> or the xhtml <br />

  8. #8
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,777
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aidos View Post
    <p>s are block-level elements and so don't need a new line between them. If you want to display something that normally appears inline in html on a new line then you can use <br> or the xhtml <br />
    No.

    Here is the answer...

    Code:
    <?php
    	echo "\n<p>Apples2</p>\n<p>Oranges2</p>\n<p>Bananas2</p>\n";
    ?>
    That will give me...

    Code:
    <p>Apples2</p>
    <p>Oranges2</p>
    <p>Bananas2</p>
    in my View Page Source.

    As far as the web page itself, right, I only need <p> or <br> to create page breaks in the web page itself.

    I was asking about \n to format my HTML.

    Thanks,



    Debbie

  9. #9
    Non-Member
    Join Date
    Dec 2010
    Location
    /home/pc
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DoubleDee View Post
    I was asking about \n to format my HTML.
    The answer is in the the link I posted in my first post in this thread.

  10. #10
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,832
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    For PHP to interpret \n as new line it needs to be inside of "" and output using an echo or print command.

    To get a new line in HTML simply start a new line.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  11. #11
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,278
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by aidos
    The answer is in the the link I posted in my first post in this thread.
    I'm not saying this to be mean, but the link says (if you can find it in all that clutter):
    This page examines the nuances of escape characters used in C and Java program coding.
    If you dig through you can find a line talking about escaping dollar signs in PHP, but... Debbie already knows that \n is the newline character she needs.

    And I'm not saying the information on the page doesn't have value, but...
    <p>
    <img src="http://merc.tv/img/wav.gif" width="16" height="16" border="0">
    <a href="sounds/poolbreak.wav">
    Sound: Pool balls breaking</a>
    <p>
    I went ARGLEBLARGLEAAAAAACKpbth!

    Stephen seems to have pointed out why Deb's original \n weren't working. I didn't see anything about quote marks in PHP on that page (but I may have missed it).

  12. #12
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,777
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stomme poes View Post
    I'm not saying this to be mean, but the link says (if you can find it in all that clutter):

    If you dig through you can find a line talking about escaping dollar signs in PHP, but... Debbie already knows that \n is the newline character she needs.

    And I'm not saying the information on the page doesn't have value, but...
    <p>
    <img src="http://merc.tv/img/wav.gif" width="16" height="16" border="0">
    <a href="sounds/poolbreak.wav">
    Sound: Pool balls breaking</a>
    <p>
    I went ARGLEBLARGLEAAAAAACKpbth!

    Stephen seems to have pointed out why Deb's original \n weren't working. I didn't see anything about quote marks in PHP on that page (but I may have missed it).
    Thank you!!

    At any rate, I figured things out, and Felgall's comments help also.

    Thanks,


    Debbie

  13. #13
    Resident curmudgeon bronze trophy gary.turner's Avatar
    Join Date
    Jan 2009
    Location
    Dallas
    Posts
    990
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think it was mentioned already, but since that is static markup there is no need for \n. Simply format the html as you want it.
    Code:
    <?php
    	// Get Configuration settings.
    	require('includes/config.inc.php');
    
    	// Set page title.
    	$page_title = 'About';
    
    	// Get Header file.
    	require('includes/header.html');
    ?>
    <h3>About</h3>
    <p>This website is about...</p>
    <p>Apples</p>
    <p>Oranges</p>
    <p>Bananas</p>
    <?php
    	// Get Footer file.
    	include ('includes/footer.html');
    ?>
    cheers,

    gary
    Anyone can build a usable website. It takes a graphic
    designer to make it slow, confusing, and painful to use.

    Simple minded html & css demos and tutorials

  14. #14
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    As you probably know now /n does nothing in html or php.
    You can use <br> 's but they are recommended not for styling or layout, but for breaking lines in content; such as, multiple stanzas in a poem.



    Quote Originally Posted by AllanP View Post
    In HTML you can use <br>.

    Writing innerHTML you might write
    var build='<p>First line<\/p>\n<p>2nd line<\/p>\n';

    If you were to look at the string you have built, say using an alert(build); the \n characters show the code in its correct position as follows:
    <p>First line</p>
    <p>2nd line</p>

    Without the \n the string would not break at the end of the </p> and would be one long line.
    Allan P, what is the two different slashes in the closing paragraph? <\/p> ?

    I have seen this, but always wonder what is its use or its purpose?

    Thanks in Advance,
    Team 1504

  15. #15
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,777
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by team1504 View Post
    As you probably know now /n does nothing in html or php.
    Wrong.

    The \n creates a carriage return in your HTML when you view the page source.

    (That was the whole purpose for this thread even though several people missed that.)

    If you look above, you'll see how I properly figured out the syntax to use \n in my PHP to display HTML the way I wanted in the HTML output.


    Allan P, what is the two different slashes in the closing paragraph? <\/p> ?

    I have seen this, but always wonder what is its use or its purpose?

    Thanks in Advance,
    Team 1504
    It is an escape character for the forward slash...



    Debbie

  16. #16
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,278
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    Team: Debbie wanted to keep her source code from becoming one long unreadable line when you View Source on her web page. So not <br> style newlines on the web page, just in the source.

    Debbie, does Gary's solution work? Manually making the breaks without characters in your PHP?

  17. #17
    Robert Wellock silver trophybronze trophy xhtmlcoder's Avatar
    Join Date
    Apr 2002
    Location
    A Maze of Twisty Little Passages
    Posts
    6,316
    Mentioned
    60 Post(s)
    Tagged
    0 Thread(s)
    XHTML considers the forward slash to be an end marker, so when writing HTML with JavaScript the forward slashes should be preceded with an escape character ("\", backward slash).

  18. #18
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,777
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stomme poes View Post
    Team: Debbie wanted to keep her source code from becoming one long unreadable line when you View Source on her web page. So not <br> style newlines on the web page, just in the source.

    Debbie, does Gary's solution work? Manually making the breaks without characters in your PHP?
    If your HTML code looks like this...

    Code:
    <p>One</p><p>Two</p><p>Three</p>
    
    <p>Four</p>
    <p>Five</p>
    <p>Six</p>
    then the Page Source will look like this...

    Code:
    <p>One</p><p>Two</p><p>Three</p>
    
    <p>Four</p>
    <p>Five</p>
    <p>Six</p>
    and your screen output will look like this...

    One

    Two

    Three

    Four

    Five

    Six

    *************************

    If your PHP code looks like this...

    Code:
    <?php
    	echo "<p>Seven</p><p>Eight</p><p>Nine</p>
    
    <p>Ten</p>
    <p>Eleven</p>
    <p>Twelve</p>";
    ?>
    then the Page Source will look like this...

    Code:
    <p>Seven</p><p>Eight</p><p>Nine</p>
    
    <p>Ten</p>
    <p>Eleven</p>
    <p>Twelve</p>
    and your screen output will look like this...

    Seven

    Eight

    Nine

    Ten

    Eleven

    Twelve
    So apparently the carriage returns do work in the PHP code.


    *************************
    HOWEVER
    ...

    If your PHP code looks like this...

    Code:
    <?php
    	echo "\n<p>Break</p>\n<p>This</p>\n<p>Single</p>\n<p>Line</p>";
    	echo "\nUp\nInto\nParts\n";
    ?>

    then the Page Source will look like this...

    Code:
    <p>Break</p>
    <p>This</p>
    <p>Single</p>
    <p>Line</p>
    Up
    Into
    Parts
    and your screen output will look like this...

    Break

    This

    Single

    Line
    Up Into Parts
    So using the \n does have its place in your tool-belt when you need to pretty-up your HTML code that is created by your PHP code.



    Debbie

  19. #19
    Robert Wellock silver trophybronze trophy xhtmlcoder's Avatar
    Join Date
    Apr 2002
    Location
    A Maze of Twisty Little Passages
    Posts
    6,316
    Mentioned
    60 Post(s)
    Tagged
    0 Thread(s)
    Yes, I agree if you are using PHP then sometimes you may use \n for the newline if you are using string and echo for pretty format of the source code output.

    For example:
    Code PHP:
    echo ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");

  20. #20
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,832
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by xhtmlcoder View Post
    For example:
    Code PHP:
    echo ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    Is there any particular reason why you put ( ) around the string?

    Code PHP:
    echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";

    works just as well and is two characters less.

    One thing you do need to watch out for with that particular statement is that PHP short tags are not turned on. If it is turned on then you'd need to write that statement like this to avoid errors:

    Code PHP:
    echo "<"."?xml version=\"1.0\" encoding=\"UTF-8\"?".">\n";
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  21. #21
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    This all makes sense now. I did not know you could change the view-scoure like that.

    I apologise for causing confusion

    and I most definatley know what escape characters are, but since I was only reading the HTML, my brain was thinking only in HTML and in HTML there are no escape characters.

    However when HTML is be written or is in js or php, escape characters must be used so that said character is not confused as a part of the js/php, but regonised as part of the HTML right?

    Thanks for helping me learn

  22. #22
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,278
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    and I most definatley know what escape characters are, but since I was only reading the HTML, my brain was thinking only in HTML and in HTML there are no escape characters.
    Yeah, we figured : ) And Debbie's very first post wasn't quite so clear... it got clearer as the thread went on.

    However when HTML is be written or is in js or php, escape characters must be used so that said character is not confused as a part of the js/php, but regonised as part of the HTML right?
    If and only if the \ in that language has special meaning otherwise.

    In Perl, \ has special meaning, but not always: using single quotes in printing may tell Perl to see it exactly as a \ character and nothing special, for example (do not interpolate).

    print 'some text with a \ shows the \';
    shows
    some text with a \ shows the \

    So it depends on the language and when that language is interpolating the \ (meaning, it needs \ to escape special characters).

  23. #23
    SitePoint Guru team1504's Avatar
    Join Date
    May 2010
    Location
    Okemos, Michigan, USA
    Posts
    732
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stomme poes View Post
    it got clearer as the thread went on.In Perl, \ has special meaning, but not always: using single quotes in printing may tell Perl to see it exactly as a \ character and nothing special, for example (do not interpolate).

    print 'some text with a \ shows the \';
    shows
    some text with a \ shows the \

    So it depends on the language and when that language is interpolating the \ (meaning, it needs \ to escape special characters).
    yeah, I started reading the entire thread and everything is making more sense.

    Also, I started learning Perl last summer and I didn't know about / in Perl. I guess that means I have not had a problems with it so far and if I do then it's for the perl folks

  24. #24
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,278
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    Also, I started learning Perl last summer and I didn't know about / in Perl.
    That forward slash / usually means "bracket for a regular expression" (also in Javascript and other languages).

    The back slash \ is used for escaping characters.

    Perl will likely let you know if you are getting into trouble with \'s or /'s : )

  25. #25
    Non-Member Girish singh's Avatar
    Join Date
    Jan 2011
    Location
    india,noida
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In HTML/XHTML the \n is not required as the </p> will automatically break the line and also we use <br> tag all are same.but <br></p> tag only use in scripting language./n new line character use all types of languages like C,C++,java etc. So <p> <br> is best suited fir html.keep it up..


Bookmarks

Posting Permissions

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