0

Possible Duplicate:
php echo vs open&close tag

For example. Is this:

<p>Hello welcome to my site</p>
<p>Your name is <? echo $name; ?>!</p>
<p>Your age is <? echo $age; ?>!</p>
<p>Your email address is <? echo $email; ?>!</p>

Faster or slower than this:

<?
echo "<p>Hello welcome to my site</p>";
echo "<p>Your name is " . $name . "!</p>";
echo "<p>Your age is " . $age . "!</p>";
echo "<p>Your email address is " . $email . "!</p>";
?>

If the site is getting millions of page views...?

Community
  • 1
  • 1
supercoolville
  • 8,636
  • 20
  • 52
  • 69

3 Answers3

5

Beware premature optimization and micro-optimization. Whilst one may be faster than the other, both are so fast that the differences are probably going to be in most practical cases irrelevant.

Chances are the biggest bottleneck in your code is database access. In most projects this is generally the case. Optimizing your queries to run quickly and return only relevant data, and optimizing your code to only query when it has to, and caching results so as not to repeat queries will yield much more performance than swapping echos for escapes from PHP.

On a personal note, I tend to favour escaping out of PHP and using HTML where possible for purely aesthetic reasons, namely the IDE recognises the HTML markup and can highlight it for me. It also makes life somewhat easier for the designers on the team who aren't very familiar with PHP.

GordonM
  • 31,179
  • 15
  • 87
  • 129
3

The first code snippet yields this Zend code:

ECHO '%3Cp%3EHello+welcome+to+my+site%3C%2Fp%3E%0A%3Cp%3EYour+name+is+'
ECHO !0
ECHO '%21%3C%2Fp%3E%0A%3Cp%3EYour+age+is+'
ECHO !1
ECHO '%21%3C%2Fp%3E%0A%3Cp%3EYour+email+address+is+'
ECHO !2                                                                                       ECHO '%21%3C%2Fp%3E%0A'

And the second one:

ECHO '%3Cp%3EHello+welcome+to+my+site%3C%2Fp%3E'
CONCAT ~0 '%3Cp%3EYour+name+is+', !0
CONCAT ~1 ~0, '%21%3C%2Fp%3E'
ECHO ~1
CONCAT ~2 '%3Cp%3EYour+age+is+', !1
CONCAT ~3 ~2, '%21%3C%2Fp%3E'
ECHO ~3
CONCAT ~4 '%3Cp%3EYour+email+address+is+', !2
CONCAT ~5 ~4, '%21%3C%2Fp%3E'
ECHO ~5

From this i conclude that first variant is actually faster, since there is no extra string copying due to CONCAT's.

arrowd
  • 33,231
  • 8
  • 79
  • 110
  • How did you produce this? Seems I have a basic gap in my understanding! – Rich Bradshaw Nov 19 '11 at 11:35
  • This is usually optimized by xdebug though – Evert Nov 19 '11 at 11:35
  • @Rich Bradshaw I've used VLD php extension. See http://pecl.php.net/package/vld – arrowd Nov 19 '11 at 11:36
  • Thank you for this, and thank you to everyone else for the comments! – supercoolville Nov 19 '11 at 11:58
  • 1
    i totally agree with arrowdodger. I just wanted to mention there is a way to avoid the use of concat and still use a similar "echo" syntax. "echo" is a function which you can assign as many parameters as you want. By using the "." concatenation operator, the string is first concatenated in its entirety and then echoed. When replacing "." with "," you generate a list of multiple parameters for the "echo" function, each printed directly to the screen. (avoiding any string copy / concatenate operation) – Kaii Nov 19 '11 at 12:22
  • I wouldn't bet on it being faster ;) If the output is buffered (it often is to some degree) you might actually do more memory copying (as you aren't concating small strings, but adding small strings to a big string). – NikiC Nov 19 '11 at 12:43
0

Even with millions of page views, chances are using echo will not be the bottle neck of performance on ANY site. If the site is has more complex components such as database queries, optimizing them first would be far more beneficial.

Sam Becker
  • 19,231
  • 14
  • 60
  • 80