在单独的行上打印数组的内容

时间:2022-01-14 15:41:47

Experimenting with arrays and wondering why the following DOESN'T seem to print the values on SEPARATE lines when I run it?

尝试使用数组并想知道为什么以下在运行它时似乎不打印SEPARATE行上的值?

<?php

$my_array = array("stuff1", "stuff2", "stuff3");

echo $my_array[0] . "\n";
echo $my_array[1] . "\n";
echo $my_array[2] . "\n";

?>

5 个解决方案

#1


2  

From my PHP textbook:

从我的PHP教科书:

One mistake often made by new php programmers (especially those from a C background) is to try to break lines of text in their browsers by putting end-of-line characters (“\n”) in the strings they print. To understand why this doesn’t work, you have to distinguish the output of php (which is usually HTML code, ready to be sent over the Internet to a browser program) from the way that output is rendered by the user’s browser. Most browser programs will make their own choices about how to split up lines in HTML text, unless you force a line break with the <BR> tag. End-of-line characters in strings will put line breaks in the HTML source that php sends to your user’s browser (which can still be useful for creating readable HTML source), but they will usually have no effect on the way that text looks in a Web page.

新的php程序员(尤其是那些来自C背景的程序员)经常犯的一个错误是试图通过在他们打印的字符串中放置行尾字符(“\ n”)来打破浏览器中的文本行。要理解为什么这不起作用,你必须从用户浏览器呈现输出的方式区分php的输出(通常是HTML代码,准备通过Internet发送到浏览器程序)。大多数浏览器程序都会自行选择如何拆分HTML文本中的行,除非您强制使用
标记换行。字符串中的行尾字符会在PHP发送给用户浏览器的HTML源代码中放置换行符(这对于创建可读的HTML源代码仍然有用),但它们通常对文本的查找方式没有影响一个网页。

The <br> tag is interpreted correctly by all browsers, whereas the \n will generally only affect the source code and make it more readable.

所有浏览器都正确解释了
标签,而\ n通常只会影响源代码并使其更具可读性。

#2


5  

This makes the trick.

这就是诀窍。

<?php
    $my_array = array("stuff1", "stuff2", "stuff3");
    foreach ( $my_array as $item ) {
        echo $item . "<br/>";
    }
?>

#3


2  

You need to print with <br/> instead of \n because the default PHP mime type is HTML, and you use <br/> to accomplish line breaks in HTML.

您需要使用
而不是\ n进行打印,因为默认的PHP mime类型是HTML,并且您使用
来实现HTML中的换行符。

For example,

例如,

<?php

$my_array = array("stuff1", "stuff2", "stuff3");

echo $my_array[0] . "<br/>";
echo $my_array[1] . "<br/>";
echo $my_array[2] . "<br/>";

?>

#4


1  

If your viewing the output in a web browser, newlines aren't represented visually. Instead you can use HTML breaks:

如果您在Web浏览器中查看输出,则不会直观地表示换行符。相反,您可以使用HTML中断:

<?php
    $my_array = array("stuff1", "stuff2", "stuff3");
    echo implode('<br>', $my_array);
?>

#5


-1  

That's because in HTML a line break is <br />, not "\n".

这是因为在HTML中,换行符是
,而不是“\ n”。

#1


2  

From my PHP textbook:

从我的PHP教科书:

One mistake often made by new php programmers (especially those from a C background) is to try to break lines of text in their browsers by putting end-of-line characters (“\n”) in the strings they print. To understand why this doesn’t work, you have to distinguish the output of php (which is usually HTML code, ready to be sent over the Internet to a browser program) from the way that output is rendered by the user’s browser. Most browser programs will make their own choices about how to split up lines in HTML text, unless you force a line break with the <BR> tag. End-of-line characters in strings will put line breaks in the HTML source that php sends to your user’s browser (which can still be useful for creating readable HTML source), but they will usually have no effect on the way that text looks in a Web page.

新的php程序员(尤其是那些来自C背景的程序员)经常犯的一个错误是试图通过在他们打印的字符串中放置行尾字符(“\ n”)来打破浏览器中的文本行。要理解为什么这不起作用,你必须从用户浏览器呈现输出的方式区分php的输出(通常是HTML代码,准备通过Internet发送到浏览器程序)。大多数浏览器程序都会自行选择如何拆分HTML文本中的行,除非您强制使用
标记换行。字符串中的行尾字符会在PHP发送给用户浏览器的HTML源代码中放置换行符(这对于创建可读的HTML源代码仍然有用),但它们通常对文本的查找方式没有影响一个网页。

The <br> tag is interpreted correctly by all browsers, whereas the \n will generally only affect the source code and make it more readable.

所有浏览器都正确解释了
标签,而\ n通常只会影响源代码并使其更具可读性。

#2


5  

This makes the trick.

这就是诀窍。

<?php
    $my_array = array("stuff1", "stuff2", "stuff3");
    foreach ( $my_array as $item ) {
        echo $item . "<br/>";
    }
?>

#3


2  

You need to print with <br/> instead of \n because the default PHP mime type is HTML, and you use <br/> to accomplish line breaks in HTML.

您需要使用
而不是\ n进行打印,因为默认的PHP mime类型是HTML,并且您使用
来实现HTML中的换行符。

For example,

例如,

<?php

$my_array = array("stuff1", "stuff2", "stuff3");

echo $my_array[0] . "<br/>";
echo $my_array[1] . "<br/>";
echo $my_array[2] . "<br/>";

?>

#4


1  

If your viewing the output in a web browser, newlines aren't represented visually. Instead you can use HTML breaks:

如果您在Web浏览器中查看输出,则不会直观地表示换行符。相反,您可以使用HTML中断:

<?php
    $my_array = array("stuff1", "stuff2", "stuff3");
    echo implode('<br>', $my_array);
?>

#5


-1  

That's because in HTML a line break is <br />, not "\n".

这是因为在HTML中,换行符是
,而不是“\ n”。