PHP: What’s the speed difference “template” vs “quote”? [duplicate]

Let’s find out:

<?php
ob_start();
$a = 0;
$time1 = microtime(true);
for ($i = 0; $i < 100000; $i++) {
    echo "<html><body>$a</body></html>";
}
$time2 = microtime(true);
for ($i = 0; $i < 100000; $i++) {
?>
<html><body><?php echo $a; ?></body></html> 
<?php
}
$time3 = microtime(true);

ob_end_clean();
echo 'Just echo: ' . ($time2 - $time1) . '<br>';
echo 'Inline PHP: ' . ($time3 - $time2) . '<br>';
?>

Result:

Just echo: 0.037185907363892
Inline PHP: 0.040054082870483

Looks like the first method is slightly faster. But the difference is so small it’s negligible and definitely not a reason to output huge blocks of HTML code by echoing strings.

Leave a Comment