php里面的html [关闭]

What is more effective in the matter of performance (and why)?

$s = "<div>";  
for($i = 0; $i < 10; $i++)  
    $s.= "<p>".$i."</p>";
$s.= "</div>";
echo $s;

or

<div>
<?php for($i = 0; $i < 10; $i++) { ?>
<p> <?php echo $i; ?> </p> 
<?php } ?>
</div>

The second option looks cleaner and it should be more efficient, as it does less work than the other: why use <?php echo "<div>"; ?>" when "<div>" is enough?.

But, being honest, the difference in performance is minuscule, almost irrelevant.

There will be no difference in the same way that there will be no difference in running around the world and running around the world plus one meter.

There is probably no difference in performance, but this way your code may be a little more readable, specially if you're going to use any other loops or conditions.

<div>
<?php for($i = 0; $i < 10; $i++): ?>
  <p> <?php echo $i; ?> </p> 
<?php endfor; ?>
</div>

If this were Javascript the second example would run literally 50 times faster due to DOM manipulation behavior, however since this is Server Side there will be no difference save the overhead of additional computation in the first example. If I had to choose, I would choose the second for that reason.

Since the answer is given and that there were comments on readability, this is my answer for readability, icya:

<?php

$s = "";
for ($i = 0; $i < 10; $i++)
    $s .= "<p>" . $i . "</p>";

$html = <<<HTML
<html>
<body>
<div>$s</div>
</body>
</html>
HTML;

echo $html;