Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 7 of 7
  1. #1
    Senior Member
    Join Date
    Aug 2011
    Posts
    227
    Member #
    29153
    Liked
    4 times
    What's the difference in using <br> and \n at the end of an echo statement.

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,715
    Member #
    5580
    Liked
    717 times
    The best way to explain it is to test it out.

    Run this test ... call this "test.php" ...
    PHP Code:
    <?php
    echo "<div id='test'>this is a test</div>\n";
    echo 
    "<div id='test'>this is a test</div>\n";
    echo 
    "<div id='test'>this is a test</div>\n";

    echo 
    "<div id='test'>this is a test</div><br>";
    echo 
    "<div id='test'>this is a test</div><br>";
    echo 
    "<div id='test'>this is a test</div><br>";
    ?>
    Now view the HTML source (on your browser) of the resulting script:

    <div id='test'>this is a test</div>
    <div id='test'>this is a test</div>
    <div id='test'>this is a test</div>
    <div id='test'>this is a test</div><br><div id='test'>this is a test</div><br><div id='test'>this is a test</div><br>

    The \n is a "literal" linefeed for the actual HTML, and it reflects that in the output.
    The <br> outputs the linefeed on the browser, but there is no "literal" linefeed in HTML.

    And likewise, this ...
    echo "<div id='test'>this is a test</div>\n";
    echo "<div id='test'>this is a test</div>\n";
    echo "<div id='test'>this is a test</div>\n";

    Will do the same thing as this ...
    echo "
    <div id='test'>this is a test</div>
    <div id='test'>this is a test</div>
    <div id='test'>this is a test</div>
    ";


  4. #3
    Senior Member
    Join Date
    Aug 2011
    Posts
    227
    Member #
    29153
    Liked
    4 times
    Seems a little useless to me. The \n that is.

  5. #4
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,715
    Member #
    5580
    Liked
    717 times
    Glenn,
    You'll have to remember it though, because you will see it used when sending
    data to some functions, not so much with "echo".

    Example
    To send headers with PHP mail(), you'll need to use it:
    From: joe@aol.com\r\n Reply-To: jim@comcast.net\r\n

    \r = return
    \n= next line (new line)


  6. #5
    Senior Member
    Join Date
    Aug 2011
    Posts
    227
    Member #
    29153
    Liked
    4 times
    Quote Originally Posted by mlseim, post: 224834
    Glenn,
    You'll have to remember it though, because you will see it used when sending
    data to some functions, not so much with "echo".

    Example
    To send headers with PHP mail(), you'll need to use it:
    From: joe@aol.com\r\n Reply-To: jim@comcast.net\r\n

    \r = return
    \n= next line (new line)
    return? What does that do?

  7. #6
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,715
    Member #
    5580
    Liked
    717 times
    These are carry-overs from the olden-days ...
    http://www.cre8asiteforums.com/forum...howtopic=43934

    I just use them when I'm told to ... not knowing why, other than, "you have to".


  8. #7
    WDF Staff AlphaMare's Avatar
    Join Date
    Oct 2009
    Location
    Montreal, Canada
    Posts
    4,570
    Member #
    20277
    Liked
    878 times
    \n forces a line break in outputted text.
    <br /> renders as text.

    Line breaks are useful if you want to echo more than one line of text in a div, say you're outputting a comment such as the one below:
    <?php
    if (strpos($_SERVER['HTTP_USER_AGENT'],'MSIE') !== FALSE) {
    echo 'You are using Internet Explorer.\nYou should try Chrome instead.\nIt is a better browser.';
    }
    ?>
    Good design should never say "Look at me!"
    It should say "Look at this." ~ David Craib


    http://digitalinsite.ca ~ my current site . . info@digitalinsite.ca ~ my email

    If you feel that someone's post helped you fix your problem, answered your question, or just made you feel better, feel free to "Like" their post. The "Like" link is at the bottom right of each post, along side the "reply" link. And if you are being helped here, try to help someone else - pass it on!


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 07:49 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com