|
Posted by Justin Koivisto on 11/04/05 23:20
windandwaves wrote:
> Hi Folk
>
> My question is:
> echo all the time vs echo at the end - what is faster
>
> Let me explain
>
> I used to write pages like this:
>
> echo "<head> ";
> {code code code}
> echo "<body>";
> {code code code, talk to the database, etc...}
> echo "<div>some more shite</div>";
> {code code code}
> echo "</html>";
>
>
> Now I do this:
>
> $v .= "<head> ";
> {code code code}
> $v .= "<body>";
> {code code code, talk to the database, etc...}
> $v .= "<div>some more shite</div>";
> {code code code}
> $v .= "</html>";
> echo $v;
>
> The advantage of the second method is that I can still manipulate the
> content before it goes out. for example, I can replace double spaces and
> tabs, alter menus, etc... using simple replace functions.
>
> Now, my question is, what is faster or better? I kind of like the latter
> option, but I do not want to use it if it makes my website (significantly)
> slower.
Consider this as a third option:
<?php
ob_start();
echo '<html>';
// other stuff here
echo '</html>';
$v=ob_get_clean();
// do stuff
echo $v;
?>
By creating a large string with what you have as your second method, you
continuously concatenate to the end of a growing string. Concatenation
takes longer than calling a language construct like "echo" to output
contents. There have been quite a few discussions here about the use of
single or double quotes as well as the use of heredoc syntax and the
performance of each.
In the end, it's a matter of preference. You can always get better
server hardware relatively inexpensively if you feel your code it taking
too long to execute. You could also purchase profiling software that
could help you find bottlenecks in your code. The method(s) you use to
output the results of your script are almost never the bottle neck, and
rarely improve performance in a noticeable manner.
--
Justin Koivisto, ZCE - justin@koivi.com
http://koivi.com
Navigation:
[Reply to this message]
|