Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between \n and <br /> in php [duplicate]

Tags:

php

I was studying php with tutorial. I recognized that in php
is sometimes use like this

echo $myString."<br />"

Suddenly, there came another in the tutorial "\n" and I got confused. Can I use it just like I use

<br />

?

So like this?

echo $mySting."\n"

Is it like a html tag? What's the difference between

<br /> and \n?
like image 899
Kim Avatar asked Dec 07 '22 01:12

Kim


2 Answers

<br /> is a HTML line-break, whereas \n is a newline character in the source code.

In other words, <br /> will make a new line when you view the page as rendered HTML, whereas \n will make a new line when you view the source code.

Alternatively, if you're outputting to a console rather than somewhere that will be rendered by a web browser then \n will create a newline in the console output.

like image 161
thexacre Avatar answered Dec 08 '22 15:12

thexacre


\n is a new line feed within the context of plain text, while <br /> is line break within the context of HTML.

HTML can interpret \n when using preformatted blocks (e.g. <pre>), but it does not by default, and should not unless there is a specific use case (like when quoting, citing poetry, or showing code).

<br /> should never be used to separate text that should otherwise be treated as a paragraph, heading or other group of text.

like image 37
zamnuts Avatar answered Dec 08 '22 15:12

zamnuts