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.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!