Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I start a new HTML line from PHP?

Tags:

html

loops

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

like image 807
Henry Yun Avatar asked Jan 15 '11 04:01

Henry Yun


1 Answers

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.

like image 59
Ryan Ballantyne Avatar answered Nov 02 '22 13:11

Ryan Ballantyne