How to start a new line of HTML from PHP?

This is the code inside my PHP loop:

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

How to get PHP to start a new line of HTML with each iteration of the loop (instead of printing everything in one line of HTML)?

+7
source share
5 answers

You need to include newlines in your output. Thus:

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

Note the double quotation marks that are required so that the escaped \ n appears as a newline instead of the alphabetic \ n.

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

Finally, as an unconnected recall, it is faster to pass multiple lines in echo commas instead of periods, for example:

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

This is because using a dot causes PHP to allocate a new string buffer, copy all individual lines to this buffer, and then output this combo string; whereas the comma makes PHP just output the lines one by one.

+22
source

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

+6
source

try the following:

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

\ n - a newline character, if in double quotes.

+5
source

I find that using single quotes is easier than using. "\ n" for example using

 echo 'text'.$var.'<br /> next line'; 

newlines are repeated if they are in single quotes

0
source

You can insert newline characters that will be displayed in the HTML source code, including literal carriage returns in your PHP lines, for example:

 $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>'; 

displays

 This is row 1 and this is row 2 

as expected, but the source code will be 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> 

Perhaps a trivial example, but when you repeat a large line with multiple lines (like a table), it is easier to debug the HTML source if these line breaks are present. Note that if you insert tabs or spaces inside these lines, the HTML code will also have these indents. Of course, the browser will hide all of them in one space, but this will simplify the execution of HTML code.

0
source

All Articles