1

这段代码:

<?php
  echo "This is the first line. \n";
  echo "This is the second line.";
?>

在同一行回显:(“这是第一行。这是第二行。”)

不应该\n具有与 基本相同的功能<br>吗?我在这里想念什么?

4

4 回答 4

5

HTML 不呈现\n,\n 将中断您的 HTML 源代码

PHP

<?php
echo "<p>This is the first line. \n";
echo "This is the second line.</p>";
?>

HTML 源代码

<p> This is the first line.
This is the second line.</p>

HTML

This is the first line. This is the second line.

PHP

<?php
echo "<p>This is the first line.";
echo "This is the second line.</p>";
?>

HTML 源代码

<p> This is the first line.This is the second line.</p>

HTML

This is the first line.This is the second line.

PHP

<?php
echo "<p>This is the first line. <br/>";
echo "This is the second line.</p>";
?>

HTML 源代码

<p> This is the first line.<br/>This is the second line.</p>

HTML

This is the first line. 
This is the second line.
于 2013-07-09T23:24:27.217 回答
3

您将 php 的输出和浏览器中的结果混合在一起。\n是换行符。当您在浏览器( Chrome中的Ctrl+ )中阅读源代码时,您可能会看到它。U

但浏览器仅<br>在网页上呈现为换行符

于 2013-07-09T23:26:35.920 回答
2
echo "This is the first line. \n";

将在您的源代码中产生一个换行符,这意味着光标当前位于下一行(与文本相爱的行)。

echo "This is the second line.";

将产生一行并将光标留在文本之后(在同一行上)。

echo "This is the second line.<br />";

将生成单行,但在呈现的 html 中包含可见的换行符。但是,在源代码中不会有换行符,所以:

echo "Line one<br />Line two";

将在 html 中呈现两行,但在源代码中呈现一行。

于 2013-07-09T23:26:34.830 回答
1
echo "This is the first line.", '<br />', PHP_EOL;

^和的HTML代码。在源代码或标签或s 中可见,或者在由 CSS (等)启用时可见,.BRENTER
ENTERPRETEXTAREAwhite-space BRHTML

于 2013-07-09T23:25:29.650 回答