Jump to content

Putting all your HTML inside PHP


littlened

Recommended Posts

--------------------------------------------------------------------------------

 

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.