php php中\n和<br />的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24026250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:07:10  来源:igfitidea点击:

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

php

提问by Kim

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

我正在学习 php 教程。我认识到在 php
中有时是这样使用的

echo $myString."<br />"

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

突然,教程中出现了另一个“\n”,我很困惑。我可以像使用一样使用它吗

<br />

?

?

So like this?

像这样?

echo $mySting."\n"

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

它像一个html标签吗?有什么区别

<br /> and \n?

回答by thexacre

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

<br />是 HTML\n换行符,而是源代码中的换行符。

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.

换句话说,<br />当您以呈现的 HTML 形式查看页面时,将换行,而当您查看源代码时,\n 将换行。

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

或者,如果您要输出到控制台而不是将由 Web 浏览器呈现的某个地方,则将\n在控制台输出中创建一个换行符。

回答by Sam

\nis a new line character(a literal new line as you would type in your code).

\n是一个新行字符(就像您在代码中键入的那样,是一个字面意义上的新行)。

$newline = "
";

var_dump($newline === "\n");
// true

You can also use the PHP constant PHP_EOL(end of line). Note, that '\n'will render \nas a literal string; you must use double quotes to have it rendered as a new line character. Also note, that my above example may actually output false..since sometimes new lines are rendered as \r\n.

您还可以使用PHP 常量PHP_EOL(行尾)。请注意,这'\n'将呈现\n为文字字符串;您必须使用双引号将其呈现为换行符。另请注意,我上面的示例实际上可能会输出 false..因为有时新行被呈现为\r\n.



<br />is an HTML element for a line break. This will show up as a new line when HTML is rendered in a browser.

<br />是一个用于换行HTML 元素。当 HTML 在浏览器中呈现时,这将显示为一个新行。



The only time that \nwill show up as a rendered line break in HTML, is when it is within a <pre>(pre-formatted text) element. Otherwise it would be the same as just formatting/indenting your HTML code:

\n在 HTML 中显示为渲染换行符的唯一时间是当它位于<pre>(pre-formatted text) element 中时。否则它将与格式化/缩进您的 HTML 代码相同:

<?php
echo "<html>\n\t<body>\n\t\tHello World!\n\t</body>\n</html>";

Outputs:

输出:

<html>
    <body>
        Hello World!
    </body>
</html>

回答by zamnuts

\nis a new line feedwithin the context of plain text, while <br />is line breakwithin the context of HTML.

\n是纯文本上下文中的换行符,而HTML 上下文中<br />换行符

HTML can interpret \nwhen 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).

HTML 可以\n在使用预先格式化的块(例如<pre>)时进行解释,但默认情况下不能解释,除非有特定用例(例如引用、引用诗歌或显示代码时),否则不应解释。

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

<br />绝不能用于分隔应被视为段落、标题或其他文本组的文本。