如何从 PHP 开始一个新的 HTML 行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4698078/
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
How do I start a new HTML line from PHP?
提问by Henry Yun
This is the code inside my PHP loop:
这是我的 PHP 循环中的代码:
echo 'Name: ' . $name . '<br/>';
How do I cause the PHP to begin a new HTML line with each iteration of the loop (instead of printing everything on a single HTML line)?
如何使 PHP 在循环的每次迭代中开始一个新的 HTML 行(而不是在单个 HTML 行上打印所有内容)?
回答by Ryan Ballantyne
You need to include newline characters in your output. Thus:
您需要在输出中包含换行符。因此:
echo 'Name: ' . $name . "<br/>\n";
Note the double quotes, which are necessary for the escaped \n to appear as a newline character instead of a literal \n.
请注意双引号,这是转义的 \n 显示为换行符而不是文字 \n 所必需的。
Another useful escape is \t, which inserts tabs into your output.
另一个有用的转义符是 \t,它将制表符插入到您的输出中。
Lastly, as an unrelated tip, it is faster to pass multiple strings to echo
with commas instead of periods, like so:
最后,作为一个不相关的提示,echo
使用逗号而不是句点传递多个字符串会更快,如下所示:
echo 'Name: ', $name ,"<br/>\n";
This is because using the dot causes PHP to allocate a new string buffer, copy all of the separate strings into that buffer, and then output this combined string; whereas the comma causes PHP to simply output the strings one after another.
这是因为使用点会导致 PHP 分配一个新的字符串缓冲区,将所有单独的字符串复制到该缓冲区中,然后输出这个组合字符串;而逗号使 PHP 简单地一个接一个地输出字符串。
回答by dqhendricks
try this:
尝试这个:
echo 'Name: ' . $name . '<br/>'."\n";
The \n is a newline character if in double quotes.
如果在双引号中, \n 是换行符。
回答by brian_d
Use the newline character in double quotes "\n"
. Single will not work.
在双引号中使用换行符"\n"
。单身是行不通的。
回答by Traveling_Monk
i find that using single quotes is easier than using ."\n" for example using
我发现使用单引号比使用 ."\n" 更容易,例如使用
echo 'text'.$var.'<br />
next line';
new line characters are echoed if they are within single quotes
如果换行符在单引号内,则回显换行符
回答by Steve GS
You can insert newline characters that will appear in HTML source code by including literal carriage returns in your PHP strings, eg:
您可以通过在 PHP 字符串中包含文字回车符来插入将出现在 HTML 源代码中的换行符,例如:
$count1 = '1';
$count2 = '2';
/* Note the opening single quote in the next line of code is closed on
the following line, so the carriage return at the end of that line is
part of the string */
echo '<table>
<tr><td>This is row</td><td>' , $count1 , '</td><td>and</td></tr>
<tr><td>this is row</td><td>' , $count2 , '</td><td> </td></tr>
</table>';
will output
会输出
This is row 1 and
this is row 2
as you'd expect, but the source code will presented as:
如您所料,但源代码将显示为:
<table>
<tr><td>This is row</td><td>1</td><td>and</td></tr>
<tr><td>this is row</td><td>2</td><td> </td></tr>
</table>
Trivial example maybe, but when you're echoing a large string with several lines (like a table), it's easier to debug the HTML source if these line breaks are present. Note that, if you indent with tabs or spaces within these strings, the HTML code will also have these indents. Of course, the browser will collapse all of them into single spaces, but it makes the HTML code easier to follow.
也许是一个简单的例子,但是当您回显包含多行(如表格)的大字符串时,如果存在这些换行符,则调试 HTML 源代码会更容易。请注意,如果在这些字符串中使用制表符或空格缩进,HTML 代码也将具有这些缩进。当然,浏览器会将它们全部折叠成单个空格,但它使 HTML 代码更易于理解。