多行块显示为一行PHP

时间:2017-11-18 18:16:19

标签: php

我只是逐字地运行一些示例PHP代码,但它在我的浏览器中作为单行输出。我期待看到新的多行。

<?php
$author = "Alfred E Newman";

echo <<<_END
This is a Headline

This is the first line.
This is the second.
- Written by $author.
_END;
?>

2 个答案:

答案 0 :(得分:3)

默认情况下,您的浏览器假定任何输出都是HTML,并且在显示HTML时,换行符被视为空格。你需要输出带有BR或P标签的HTML来强制换行你可以发送一个内容类型的标题来告诉浏览器你发送的输出是纯文本。

<?php
$author = "Alfred E Newman";

// tell the browser that your output is plain text
header("Content-Type: text/plain");
echo <<<_END
This is a Headline

This is the first line.
This is the second.
- Written by $author.
_END;
?>

答案 1 :(得分:0)

<?php
$author = "Alfred E Newman";

$str = "This is a Headline

This is the first line.
This is the second.
- Written by $author.
";

echo nl2br($str);
?>

会给你你需要的东西;