PHP:尝试用“\n”创建一个新行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2674703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:18:37  来源:igfitidea点击:

PHP: trying to create a new line with "\n"

phpnewline

提问by ziiweb

i'm writing this:

我在写这个:

echo "foo";
echo "\n";
echo "bar";

and "bar" is not written in the line below.

并且“bar”没有写在下面的行中。

What am i doing wrong?

我究竟做错了什么?

Javi

哈维

回答by Tomalak

Newlines in HTML are expressed through <br>, not through \n.

HTML 中的换行符是通过 来表示的<br>,而不是通过\n

Using \nin PHP creates a newline in the source code, and HTML source code layout is unconnected to HTML screen layout.

\n在 PHP 中使用会在源代码中创建一个换行符,并且 HTML 源代码布局与 HTML 屏幕布局无关。

回答by Cups

If you want a new line character to be inserted into a plain text stream then you could use the OS independent global PHP_EOL

如果您希望将换行符插入纯文本流中,则可以使用独立于操作系统的全局 PHP_EOL

echo "foo";
echo PHP_EOL ;
echo "bar";

In HTML terms you would see a newline between foo and bar if you looked at the source code of the page.

在 HTML 术语中,如果您查看页面的源代码,您会在 foo 和 bar 之间看到换行符。

ergo, it is useful if you are outputting say, a loop of values for a select box and you value having html source code which is "prettier" or easier to read for yourself later. e.g.

因此,如果您要输出一个选择框的值循环,并且您重视拥有“更漂亮”或更易于以后自己阅读的 html 源代码,这将很有用。例如

foreach( $dogs as $dog )
echo "<option>$dog</option>" . PHP_EOL ;

回答by Yacoby

If you want to write plain text, you must ensure the content type is set to Content-Type: text/plain. Example:

如果要编写纯文本,则必须确保将内容类型设置为Content-Type: text/plain。例子:

header('Content-Type: text/plain');

If you are dealing with HTML, you have two options. One is to inset a new line using <br>(Or <br />for XHTML). The other is to put the plain text in a <pre>element (In this case "pre" stands for preformatted).

如果您正在处理 HTML,您有两种选择。一种是使用<br>(Or <br />for XHTML)插入一个新行。另一种是将纯文本放在一个<pre>元素中(在这种情况下,“pre”代表预格式化)。

回答by Daniel A. White

PHP generates HTML. You may want:

PHP 生成 HTML。你可能想要:

echo "foo";
echo "<br />\n";
echo "bar";

回答by Andrei R

if your text has newlines, use nl2br php function:

如果您的文本有换行符,请使用 nl2br php 函数:

<?php
$string = "foo"."\n"."bar";
echo nl2br($string);
?>

This should look good in browser

这在浏览器中应该看起来不错

回答by Mark Elliot

Assuming you're viewing the output in a web browser you have at least two options:

假设您在 Web 浏览器中查看输出,您至少有两个选择:

  1. Surround your text block with <pre>statements

  2. Change your \nto an HTML <br>tag (<br/>will also do)

  1. <pre>语句包围你的文本块

  2. 将您更改\n为 HTML<br>标记(<br/>也可以)

回答by flau

Since it wasn't mentioned, you can also use the CSS white-space property

由于未提及,您还可以使用 CSS white-space 属性

body{
    white-space:pre-wrap;
}

Which tells the browser to preserve whitespace so that

这告诉浏览器保留空格,以便

<body>
    <?php
        echo "hello\nthere";
    ?>
</body>

Would display

会显示

hello
there

回答by rashedcs

We can use \n as a new line in php.

我们可以在 php 中使用 \n 作为新行。

Code Snippet :

代码片段:

 <?php
  echo"Fo\n";
  echo"Pro";
?>

Output:

输出:

Fo
Pro


回答by eeyore

This answer was so obvious and it took me forever to figure out:

这个答案太明显了,我花了很长时间才弄明白:

echo "foo
bar";

I know that looks like it's wrapping. It's not. What I did is I literally hit returnhalfway through the string, between fooand bar. It creates a new line in the HTML source and makes your PHP look horrible. This was in Linux/Apache.

我知道这看起来像是在包装。它不是。我所做的是return在字符串的中途,在foo和之间bar。它在 HTML 源代码中创建了一个新行,并使您的 PHP 看起来很糟糕。这是在 Linux/Apache 中。

回答by Matt

It will be written on a new line if you examine the source code of the page. If you want it to appear on a new line when it is rendered in the browser, you'll have use a <br />tag instead.

如果您检查页面的源代码,它将被写在一个新行上。如果您希望它在浏览器中呈现时显示在新行上,则可以改用<br />标签。