PHP新行字符(\n)不工作

Pad*_*wan 2 php

  • 代码是双引号,仍然无法正常工作.
  • 我已经读过这篇文章了
  • 我正在使用Atom而且我在localhost上(如果这有任何区别)
  • 我重新下载了Atom(如果设置发生了什么事情)并没有帮助

这是代码:

<?php
$firstName = 'David';
$lastName = "Powers";
$title = '"The Hitchhiker\'s Guide to the Galaxy"';
$author = 'Douglas Adams';
$answer = 42;
$newLines = "\r\n\r\n";

$fullName = "$firstName $lastName |";
$book = "$title by $author";

$message = "Name: $fullName $newLines";
$message .= "Book: $book \r\n\r\n";
$message .= "Answer: $answer";

echo $message;
echo "Line 1\nLine 2";
Run Code Online (Sandbox Code Playgroud)

输出是一行,但当我查看源时,新行正在工作

Name: David Powers | Book: "The Hitchhiker's Guide to the Galaxy" by Douglas Adams Answer: 42Line 1 Line 2
Run Code Online (Sandbox Code Playgroud)

Pra*_*man 5

如果你正在学习PHP 5 For Dummies书,那么这是你要学习的第一件事.HTML不尊重新行或制表符或多个空格字符.你必须使用<br />新线.

预习
*来自珍妮特瓦拉德的PHP 5 For Dummies.

将您的代码更改为:

<?php
$firstName = 'David';
$lastName = "Powers";
$title = '"The Hitchhiker\'s Guide to the Galaxy"';
$author = 'Douglas Adams';
$answer = 42;
$newLines = "<br /><br />";

$fullName = "$firstName $lastName |";
$book = "$title by $author";

$message = "Name: $fullName $newLines";
$message .= "Book: $book <br /><br />";
$message .= "Answer: $answer";

echo $message;
echo "Line 1<br />Line 2";
Run Code Online (Sandbox Code Playgroud)

如果您只是选择基于文本的布局,则可以将标题设置为浏览器以将其视为文本文件.为此,您需要:

header("Content-type: text/plain");
Run Code Online (Sandbox Code Playgroud)

这将呈现没有任何HTML.