echo("<li>Hello</li>\n");
seems to work fine for putting a new line in the HTML.
Is \r
also required? For instance, echo("<li>Hello</li>\r\n");
Thanks
echo("<li>Hello</li>\n");
seems to work fine for putting a new line in the HTML.
Is \r
also required? For instance, echo("<li>Hello</li>\r\n");
Thanks
The newline in this case is not because of the \n
, but because of the <li>
. HTML transforms Whitespace into a single space, unless you use something like <pre>
.
If you just want your HTML to show up as new lines in the source (or to look "pretty" as you stated in a comment you made
) without having everything all clumped in single lines of code, concatenate the last lines of code using . "\n"
I.e.: echo("<li>Hello</li>" . "\n");
and echo "<table>" . "\n";
etc. that way you'll get nicely formatted and aligned HTML.
Using <br>
will print <br>
in the HTML source, while using \n
will not.
For example:
Using:
echo("<li>Hello</li>" . "\n");
echo("<li>Hello again</li>" . "\n");
echo("<li>Hello to you too</li>" . "\n");
The HTML source will be:
<li>Hello</li>
<li>Hello again</li>
<li>Hello to you too</li>
As opposed to using <br>
echo("<li>Hello</li>" . "<br>");
echo("<li>Hello again</li>" . "<br>");
echo("<li>Hello to you too</li>" . "<br>");
The HTML source will be:
<li>Hello</li><br><li>Hello again</li><br><li>Hello to you too</li><br>
This makes it harder to go through HTML source in order to troubleshoot/debug etc.
That is only three lines of code; imagine having dozens or hundreds of lines?
Footnotes:
Using \r
is perfectly fine for echoing and will not affect your code if added or omitted; however you're just using more characters than is required when wanting to get clean well-formatted and aligned HTML source.
Just \n
will suffice; for echo'ed output.
Comments have already been given under your original question in regards to using \r
for the purpose and relation to files under Windows and older versions of MAC, therefore I won't repeat myself.
No, it is never required. In fact the newline isn't required in HTML either.
Use the PHP_EOL
constant and PHP will use whatever is native on that platform.
See http://www.php.net/manual/en/reserved.constants.php
In HTML You have always to use
"<br />"
<?php echo "<div>HELLO</div><br />"; ?>
BUT, I had this problem to save a text on a file.
On windows just the \n to break line didn't work, when I opened the file in a text editor all the text was in the same row, so I had to use \n\r
Well, the various operating systems use different newline characters, but in practice browsers understand most of the newline conventions, i.e. \n
, \r
and \r\n
.
3.7.1 Canonicalization and Text Defaults
[...]
When in canonical form, media subtypes of the "text" type use CRLF as the text line break. HTTP relaxes this requirement and allows the transport of text media with plain CR or LF alone representing a line break when it is done consistently for an entire entity-body. HTTP applications MUST accept CRLF, bare CR, and bare LF as being representative of a line break in text media received via HTTP. [...]
Source: https://www.ietf.org/rfc/rfc2616.txt (emphasis by me). LF
stands for "Line Feed" and is the \n
character. CR
stands for "Carriage Return" and is the \r
character.
The problems with newline characters will only come up if you open the source code in some text editors. For instance, Linux newlines (\n
) may cause the text to be displayed on one line in Notepad (which uses Windows' \r\n
newline). This may not happen in newer version of Notepad, but it is just an example.
To force a new line use this:
echo("<li>Hello</li> <br />");
NOTE: A <li>
element is by default a HTML block element, so a new line is generated automatically.
If you had a stylesheet with the following code
li {
display: inline; /*changes block-Element into an inline-Element*/
}
the <br />
would be needed to force a new line.
Click here if you want to know more about block and inline elements in HTML.
` will print `
` in the source, while `\n` won't.