Reputation: 3555
I don't really get it: what's the purpose of a new line character?
If I do this:
<?php
echo "This is a test. \n";
echo "This is another test.";
?>
Code results in both sentences being in the same line. Why doesn't the \n causes the second sentence being in second line?
The sentences are each in it's own line, if I do:
<?php
echo "This is a test. <br>";
echo "This is another test.";
?>
But I have also seen people do this:
<?php
echo "This is a test. <br>\n";
echo "This is another test.";
?>
Which essentially results in the same output as the second code snippet. Someone care to explain this?
Upvotes: 5
Views: 2745
Reputation: 40675
as you have observed there are different ways to create a new line.
<br />
this is not a new line character, this is an XHTML tag which means, it works in XHTML. correctly speaking it is not a new line character but the tag makes sure, one is inserted = it forces a line break. closing tag is mandatory.
<br>
this is a HTML tag which forces a line break. closing tag is prohibited.
\n
is an escape sequence for the ASCII new line char LF. A common problem is the use of '\n' when communicating using an Internet protocol that mandates the use of ASCII CR+LF for ending lines. Writing '\n' to a text mode stream works correctly on Windows systems, but produces only LF on Unix, and something completely different on more exotic systems. Using "\r\n" in binary mode is slightly better, as it works on many ASCII-compatible systems, but still fails in the general case. One approach is to use binary mode and specify the numeric values of the control sequence directly, "\x0D\x0A".
is a php new line constant which is replaced by the correct system dependent new line.
so the message is, use everything in it's right place.
Upvotes: 9
Reputation: 4434
\n
is code based
<br />
is HTML tag based
There is a clear distinction between the two.
Upvotes: 3
Reputation: 1795
<br />
is also useless if you're running a script from the command line.
$ php -f file.php
Output <br />$
I know not too many people use PHP from the command line, but it does come up:
file.php:
<?php
print "Output\n";
?>
At the command line:
$ php -f file.php
Output
$
Upvotes: 0
Reputation: 37
Although it also has uses when writing web-based scripts, keep in mind PHP is more than a web engine; it also has a CLI where the br tag is useless.
Upvotes: 1
Reputation: 3027
The new line character is is useful for string functions.
For example:
explode( '\n' , $input );
Would split a string by a new line.
str_replace( '\n' , '<br />' , $input );
Would replace every newline in $input with a br tag.
Also because PHP also has a CLI, \n is useful for formatting:
eg.
echo 'Hello world';
Would, in the CLI, output;
Hello worldphp>
echo 'Hello world' . "\n";
would output;
Hello world
php>
Upvotes: 1
Reputation: 300825
The HTML standard treats a line break as just another white space character, which is why the <br>
tag exists. Note however a line break will work within a <pre>
tag, or an element with the white-space:pre CSS style.
The third example is just to make "pretty" HTML: it makes it easier to "view source" and check it by eye. Otherwise, you have a big long string of HTML.
Upvotes: 38
Reputation: 7851
HTML does not care about new lines in the source code, you can put it all in one line. It will only interpret <br />
as a new line. You should use an \n to beautify your HTML-output though, but the better way is to not output it with PHP, but to use it in the HTML itself and only embed PHP stuff into it, like this:
<ul id="menu">
<?php foreach ($menu_items as $item): ?>
<li>
<a href="<?= htmlspecialchars($item['link']) ?>" title="<?= htmlspecialchars($item['title']) ?>">
<?= htmlspecialchars($item['title']) ?>
</a>
</li>
<?php endforeach; ?>
</ul>
That way you won't have to bother with formatting inside PHP, but you automagically have it, by design, in HTML. Also, you seperate Model-logic and View-logic from each other like this and leave the output to your HTTP Server rather than the PHP engine.
Upvotes: 2
Reputation: 144947
The new line character is useful otherwise, such as in a PDF. You're correct that the new line character has very little do with HTML as other people have said, it's treated as another while space character. Although it is useful inside the <pre>
tag. It can also be used to format the HTML output to make it easier to read. (It's a little annoying to try to find a piece of HTML in a string that's 1000 characters wide.)
The new line character is also useful when storing data in the database. Usually you want to store the data without HTML special characters such as <br />
so that it can be easily used in other formats (again, such as PDF). On output, you want to use the nl2br()
function to convert the new lines to <br />
s.
Upvotes: 1
Reputation: 1377
The correct XHTML syntax for it would be
echo "This is the test code <br />\n";
The <br />
renders a new line onscreen, the "\n" renders a new line in the source coed
Upvotes: 1
Reputation: 229583
That's because you're creating HTML and view it in a browser, and whitespace is more or less ignored there. Ten spaces don't produce a bigger gap than one space, but that doesn't mean that the space character doesn't work. Try setting the content type to text/plain
or look at the HTMLs source to see the effect of the newline.
Upvotes: 1
Reputation: 5078
When the html is rendered, only the "<br />
" renders the break line. However the markup is much easier to read when "<br />\n
" is printed, so that everything is not in one long line.
Upvotes: 4
Reputation: 46415
Your problem is the way html is rendered. If you look in the source code, the first example will have the two lines on separate lines, but the browser does not see line breaks as breaks that should be displayed. This allows you to have a long paragraph in your source:
rghruio grgo rhgior hiorghrg hg rgui
ghergh ugi rguihg rug hughuigharug
hruauruig huilugil guiui rui ghruf hf
uhrguihgui rhguibuihfioefhw8u
beruvbweuilweru gwui rgior
That would only wrap as the browser needed it to, allowing it to be easily editable at the right line length, but displayed at any resolution.
Upvotes: 2
Reputation: 12900
<br>
will give you a new line in the user's view; \n
will give you a new line in source code, ie. developer's view.
Upvotes: 6