Web Hosting Talk







View Full Version : which is best coding?


PortalMelt
09-11-2002, 01:09 PM
Which is Best Coding?
Each of these sets of code will produce the same results in the display.

Question: When carried out in a large application, which is better? Is there a performance difference in the two different methods?

The benefit of the first one is that I can control all of the display (the HTML) in Dreamweaver and actually see what I'm doing, but I wonder if there is a performance hit or some other problem.

Thanks for your insights.
Comments please.
Jeff


<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td><?php echo $item1; ?></td>
<td><?php echo $item2; ?></td>
<td><?php echo $item3; ?></td>
</tr>
<tr>
<td><?php echo $item4; ?></td>
<td><?php echo $item5; ?></td>
<td><?php echo $item6; ?></td>
</tr>
</table>

<?php
echo "<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">";
."<tr>"
."<td>".$item1."</td>"
."<td>".$item2."</td>"
."<td>".$item3."</td>"
."</tr>"
."<tr>"
."<td>".$item4."</td>"
."<td>".$item5."</td>"
."<td>".$item6."</td>"
."</tr>"
."</table>";
?>

prosayist
09-11-2002, 01:15 PM
actually, I'm not quite sure of the perf diff myself, but I'd be likely to guess that <?php
echo "<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
<td>".$item1."</td>
<td>".$item2."</td>
<td>".$item3."</td>
</tr>
<tr>
<td>".$item4."</td>
<td>".$item5."</td>
<td>".$item6."</td>
</tr>
</table>";
?>
wouldn't cause any parse errors :D

PortalMelt
09-11-2002, 01:19 PM
That's true, but not what I was asking.

In my first example, PHP is opened and closed only when needed and not used to echo the html. This leaves the html exposed to Dreamweaver.

In my second example, PHP is opened once and generates everthing. This hides the html from Dreamweaver.

Comments please.
Jeff

Alturus
09-11-2002, 01:24 PM
The first method is generally accepted as better programming practice.

prosayist
09-11-2002, 01:27 PM
really I understood that, just pointing out a trivial fact [edit: not a 'face'].. sorry for not applying myself to find your answer;
check here -> http://p2p.wrox.com/archive/php_howto/2002-07/28.asp

or here -> http://www.google.com/search?sourceid=navclient&q=performance+difference+html+%3C%3Fecho+php :)

Alturus
09-11-2002, 01:42 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
<style type="text/css">
.main {
text-align: center;
width: 33%;
float: left;
border: 1px solid #000000;
}
</style>
</head>
<body>


<div class="main"><? echo $var1; ?></div>
<div class="main"><? echo $var2; ?></div>
<div class="main"><? echo $var3; ?></div>

<div class="main"><? echo $var4; ?></div>
<div class="main"><? echo $var5; ?></div>
<div class="main"><? echo $var6; ?></div>


</body>
</html>



Is a little better code : )


But in all seriousness, using PHP to parse a chunk of code (the table tages) will use more processing time than just processing the variable names. Plus it works better for you while using DW.

PortalMelt
09-11-2002, 01:53 PM
Thanks.

prosayist: I wasn't mad, just trying to keep things on track. Thanks for the followup answer.

Alturus: Too funny, and yes I realize that. Thanks for the humor and the feedback. This seems to be the concesus I am gettin gin the diff places and people I have asked.


I see A LOT of code done the second way. Makes you go Hmmmm. . .

Rich2k
09-11-2002, 03:17 PM
Originally posted by Alturus

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
<style type="text/css">
.main {
text-align: center;
width: 33%;
float: left;
border: 1px solid #000000;
}
</style>
</head>
<body>


<div class="main"><?= $var1; ?></div>
<div class="main"><?= $var2; ?></div>
<div class="main"><?= $var3; ?></div>

<div class="main"><?= $var4; ?></div>
<div class="main"><?= $var5; ?></div>
<div class="main"><?= $var6; ?></div>


</body>
</html>





Actually you could simplify it even further. Personally I don't use short tags like that... but I believe that's the format.

idlenut
09-11-2002, 08:58 PM
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td><?=$item1?></td>
<td><?=$item2?></td>
<td><?=$item3?></td>
</tr>
<tr>
<td><?=$item4?></td>
<td><?=$item5?></td>
<td><?=$item6?></td>
</tr>
</table>
that is the shortest way to print variables while jumping in and out of PHP mode

prosayist
09-13-2002, 03:45 AM
.. ya might also look here => http://evolt.org/article/PHP_Guidelines/17/26440/index.html

idlenut's last comment is marginally faster