PDA

View Full Version : Putting all your HTML within PHP



Littlened
30 Jul 2008, 10:42 AM
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.

mick.dodd
30 Jul 2008, 05:51 PM
i can see no advantage, its just lazy coding