Embeding HTML into PHP

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
diG1tY
Forum Newbie
Posts: 17
Joined: Sun Feb 01, 2009 2:50 pm

Embeding HTML into PHP

Post by diG1tY »

Hello. In a book by WROX press I read that it's not a good idea embedding HTML into PHP where it is not needed. It states basiclly that it's better to design your layout in HTML and parse things to PHP that are only needed. Is this true ? Does it make any difference if you write :

Code: Select all

 
<p>Some text and tags</p>
versus

Code: Select all

<?php echo "<p>Some text and tags</p>?>
Is it better to put only the things related to PHP or it doesn't matter how you end up embeding "on random'?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Embeding HTML into PHP

Post by Benjamin »

I'd say a good rule to go by is that you should not use PHP to print static data.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Embeding HTML into PHP

Post by Benjamin »

lkjkorn19 wrote:I guess it depends on the context as well. If you're going to generate a list, it would be ridiculous to load up data (in PHP), stop PHP, type that one "<h1>List</h1><ul>" line, get back into PHP and start listing things up.
If you're to do a longer paragraph that explains something (for example), exiting PHP is advised just principally for overview reasons. Like that, you don't have to worry about escaping quotes and whatnot. :)
Actually, in templating you are not supposed to incorporate logic, other than simple if, else, for and foreach loops. So your controller would generate the list using data it retrieves from the model. Then in your template it would look like this:

Code: Select all

 
<ul>
  <?php foreach ($listItems as $item): ?>
    <li><?php echo $item; ?></li>
  <?php endforeach; ?>
</ul>
 
Post Reply