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
| SitePoint Sponsor |





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



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.

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="^$">





I'm not getting this.
Here is my code...
If I take out my two \n's then one line in the View Code I get...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'); ?>
<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


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>

<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 />I'm not getting this.





No.
Here is the answer...
That will give me...Code:<?php echo "\n<p>Apples2</p>\n<p>Oranges2</p>\n<p>Bananas2</p>\n"; ?>
in my View Page Source.Code:<p>Apples2</p> <p>Oranges2</p> <p>Bananas2</p>
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


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="^$">





I'm not saying this to be mean, but the link says (if you can find it in all that clutter):Originally Posted by aidos
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.This page examines the nuances of escape characters used in C and Java program coding.
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).
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.cheers,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'); ?>
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




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.
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





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.
It is an escape character for the forward slash...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
Debbie





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?


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).
};-) http://www.xhtmlcoder.com/
Thinking Web: Voices of the Community
> March 2013 - SitePoint forums: Spot the Error 3: Calling all Sleuths! Winner Announced!... She knows how to spot simple <code> errors but do you?





If your HTML code looks like this...
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...Code:<p>One</p><p>Two</p><p>Three</p> <p>Four</p> <p>Five</p> <p>Six</p>
One
Two
Three
Four
Five
Six
*************************
If your PHP code looks like this...
then the Page Source will look like this...Code:<?php echo "<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...Code:<p>Seven</p><p>Eight</p><p>Nine</p> <p>Ten</p> <p>Eleven</p> <p>Twelve</p>
So apparently the carriage returns do work in the PHP code.Seven
Eight
Nine
Ten
Eleven
Twelve
*************************
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...
and your screen output will look like this...Code:<p>Break</p> <p>This</p> <p>Single</p> <p>Line</p> 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.Break
This
Single
Line
Up Into Parts
Debbie


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");
};-) http://www.xhtmlcoder.com/
Thinking Web: Voices of the Community
> March 2013 - SitePoint forums: Spot the Error 3: Calling all Sleuths! Winner Announced!... She knows how to spot simple <code> errors but do you?

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="^$">




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![]()





Yeah, we figured : ) And Debbie's very first post wasn't quite so clear... it got clearer as the thread went on.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.
If and only if the \ in that language has special meaning otherwise.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?
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).





That forward slash / usually means "bracket for a regular expression" (also in Javascript and other languages).Also, I started learning Perl last summer and I didn't know about / in Perl.
The back slash \ is used for escaping characters.
Perl will likely let you know if you are getting into trouble with \'s or /'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