November 18, 2010
PHP performance when printing
Question by Joel
Is there any difference between:
echo "<p>" . $var . "</p>";
and
<p> <?php echo $var; ?> </p>
when it comes to performance?
Joel
Answer by laurencek
The second is slightly quicker as it doesn’t have to concatenate the strings together.
But really you’re only going to see an increase in performance if this is repeated a huge number of times.
Also, as a slight side point, using the multiple parameters of the echo function, such as:
echo("<p>",$var,"</p>");
is also quicker than concatenating the string.
Answer by Starx
Even faster is this
<p><?=$var?></p>
Using <p> <?php echo $var; ?> </p>
than echo "<p>" . $var . "</p>";
reduces server side operation. Even though in this case it is neglegible, it does have a difference.