如何从PHP启动新的HTML行?

时间:2022-01-03 21:15:24

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行上打印所有内容)?

5 个解决方案

#1


22  

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 '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只是一个接一个地输出字符串。

#2


6  

Use the newline character in double quotes "\n". Single will not work.

在双引号“\ n”中使用换行符。单身无效。

#3


5  

try this:

尝试这个:

echo 'Name: ' . $name . '<br/>'."\n";

The \n is a newline character if in double quotes.

如果是双引号,\ n是换行符。

#4


0  

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

如果新行字符在单引号内,则会回显它们

#5


0  

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>&nbsp;</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>&nbsp;</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代码更容易理解。

#1


22  

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 '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只是一个接一个地输出字符串。

#2


6  

Use the newline character in double quotes "\n". Single will not work.

在双引号“\ n”中使用换行符。单身无效。

#3


5  

try this:

尝试这个:

echo 'Name: ' . $name . '<br/>'."\n";

The \n is a newline character if in double quotes.

如果是双引号,\ n是换行符。

#4


0  

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

如果新行字符在单引号内,则会回显它们

#5


0  

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>&nbsp;</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>&nbsp;</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代码更容易理解。