php Newlines vs html break

时间:2013-07-09 23:22:46

标签: php

此代码:

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

在同一行回显所有:(“这是第一行。这是第二行。”) \n不应该与<br>具有基本相同的功能吗?我在这里缺少什么?

4 个答案:

答案 0 :(得分: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.

答案 1 :(得分:3)

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

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

答案 2 :(得分: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中呈现两行但在源中呈现一行。

答案 3 :(得分:1)

echo "This is the first line.", '<br />', PHP_EOL;

^ HTML 代码BRENTERENTER在源代码或PRE代码中可见,或{ {1}},或由CSS TEXTAREA启用(等) ,而 white-spaceBR中的换行符