Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What good is new line character?

Tags:

html

php

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?

like image 621
ark Avatar asked Feb 16 '09 21:02

ark


People also ask

What is the importance of new line character?

The newline character is important in computer programming, since it allows programmers to search for line breaks in text files. For example, if a data file lists one element per line, the items can be delimited by newline characters.

Is new line a special character?

Operating systems have special characters denoting the start of a new line. For example, in Linux a new line is denoted by “\n”, also called a Line Feed. In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF.

What signifies a new line?

Definitions of new line. the operation that prepares for the next character to be printed or displayed as the first character on the next line.

What is the LF character?

LF = Line Feed ( \n , 0x0A in hexadecimal, 10 in decimal) — moves the cursor down to the next line without returning to the beginning of the line.


1 Answers

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.

like image 99
Paul Dixon Avatar answered Sep 28 '22 09:09

Paul Dixon