且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何从PHP开始一个新的HTML行?

更新时间:2023-12-04 13:44:58

您需要在输出中包含换行符。因此:

  echo'Name:'。 $名称。 &LT峰; br /> \\\
;

请注意双引号,它是转义的\ n显示为换行符所必需的字符\\\



另一个有用的转义是\t,它将标签插入到输出中。



echo 的速度会更快,如下所示:
  echo'Name:',$ name,< br /> \\\
;

这是因为使用点导致PHP分配一个新的字符串缓冲区,复制所有单独的字符串进入该缓冲区,然后输出这个组合字符串;而逗号导致PHP只是一个接一个地输出字符串。


This is the code inside my PHP loop:

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)?

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.

Another useful escape is \t, which inserts tabs into your output.

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.