Call
608.845.2900
Visit
330 Locust Dr.
Verona, WI 53593
Message
info@powderkegwebdesign.com
Powderkeg Web Design
Menu
Message Us
Nick
Nick

Cut down on echoing out HTML with segregated loops

April 04, 2014

Sometimes  you have to echo out a lot of html in PHP. Imagine getting a long list of results from a database query where you want to display the information on the page. You want to encase each item in a few div tags with classes along with a link and an image. To do this straight up, it would look something like this:

 That’s a lot of non formatted code and a lot of .’s to keep track of. Yes the code will work, but there is a better way to make it easier to read and modify. With the same information from above, we can instead code it out like this:

That piece of code basically starts a foreach loop, then closes the php tag. It then renders your html code and ends the foreach in another php tag after it. This allows you to code out actual html code and format it a lot more easily. Then you just have to open up a php tag, echo out a specific piece of information, then close that php tag. You don’t have to concatenate  a bunch of statements, and it makes for cleaner code.

You can also do the same thing for for loops, if statements, and while loops. Just make sure you close anything you open or you will get yelled at by the server!

Nick
Nick
Lead Developer

Nick is an ambitious and versatile developer with a wide range of talents. He is an efficient coder that utilizes his resources in order to achieve both his and the clients’ goals. His Alma Mater is the University of Wisconsin – Stevens Point where he graduated with honors in Web and Digital Media Development.