littlened Posted July 31, 2008 Share Posted July 31, 2008 -------------------------------------------------------------------------------- I've been building websites for about 10 year now, and generally when I do, I tend to keep as much HTML out of the PHP as I can. i.e., if I wanted to loop through an array and generate a list item I would... <html> <title>Title in here</title> <head> </head> <body> <h1>A list of stuff</h1> <select name="list"> <?php $listArray = array('item1','item2','item3','item4'); foreach ($listArray as $listArray) { ?> <option><?= $listArray['title']; ?></option> <?php } // END foreach ?> </select> </body> </html> Recently I've been working on a project which involved working on someone elses code, and I've found the whole thing absolutely terrible to work with. This guy seems to put as much HTML as possible within PHP code....like this.... <html> <title>Title in here</title> <head> </head> <body> <?php echo "<h1>A list of stuff</h1>"; echo "<select name=\"list\">"; $listArray = array('item1','item2','item3','item4'); foreach ($listArray as $listArray) { echo "<option><?= $listArray['title']; ?></option>"; } // END foreach echo "</select>"; ?> </body> </html> So I'm wondering, is it perfectly normal, which way is best? I find my way easy because when I look at a layout in Dreamweaver, I can see where the list item is in a div or table. If I had a list item in a table, I could clearly see it, but this other guy would put the list item and the table inside php echo's. Am I right in thinking that it's unnecessary php code for PHP to parse? Or is there some major benefit in doing it the other guys way. Quote Link to comment Share on other sites More sharing options...
MasterACE14 Posted July 31, 2008 Share Posted July 31, 2008 heredoc syntax is probably what you're after. google it. Its worth looking into. Quote Link to comment Share on other sites More sharing options...
unkwntech Posted July 31, 2008 Share Posted July 31, 2008 I've been developing PHP driven sites for a while and I definitely prefer to keep as little HTML in the PHP. <body> <?php echo "<h1>A list of stuff</h1>"; echo "<select name=\"list\">"; This is ridiculous and I see it all the time. Quote Link to comment Share on other sites More sharing options...
littlened Posted July 31, 2008 Author Share Posted July 31, 2008 Thanks, it's nice to hear someone agrees with me. I just cant get it into my head why people would do it that way, it's so messy and come the end of the project there could could be hundreds of extra lines of code for PHP to parse because of all the echo's. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.