简体   繁体   English

如何从 echo 获取 PHP output 中的换行符?

[英]How do I get a newline in the PHP output from echo?

I am generating a large amount of HTML code with PHP using echo and it is all appearing on one line.我正在使用echo使用 PHP 生成大量 HTML 代码,它们都出现在一行上。

I understand that HTML ignores whitespace, and there are thousands of answers which point out that you need <BR> or a block element.我知道 HTML 会忽略空格,并且有成千上万的答案指出您需要<BR>或块元素。 But that is not my question.但这不是我的问题。

My problem is that is very hard to debug my HTML source code when it is all on one line.我的问题是,当我的 HTML源代码全部在一条线上时,很难调试它。 I am using Windows 7 and the Firefox browser tool Page Source to show the source code .我正在使用 Windows 7 和 Firefox 浏览器工具Page Source来显示源代码

For a simple example, if the text editor I use shows举个简单的例子,如果我使用的文本编辑器显示

<HTML><BODY>
Hello, World!
</BODY></HTML>

Then the browser source code tool shows exactly that too, and when I generate it with PHP like然后浏览器源代码工具也显示了这一点,当我使用 PHP 生成它时

<?php
echo "<HTML><BODY>";
echo "Hello, World!";
echo "</BODY></HTML>";
?>

then the browser tool shows, as you would expect,然后浏览器工具显示,如您所料,

<HTML><BODY>Hello, World!</BODY></HTML>

I want to break the lines, and have tried我想打破界限,并尝试过

<?php
echo "<HTML><BODY>\n";
echo "Hello, World!\n";
echo "</BODY></HTML>\n";
?>

and with "\r\n" and with "\xA" and also like this"\r\n""\xA"也像这样

<?php
echo "<HTML><BODY>" . PHP_EOL;
echo "Hello, World!" . PHP_EOL;
echo "</BODY></HTML>" . PHP_EOL;
?>

yet the content stays resolutely on a single line.但是内容坚决地保持在一行上。

This works on windows这适用于 windows

<?php
echo "<HTML><BODY>
";
echo "Hello, World!
";
echo "</BODY></HTML>
";
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM