By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,873 Members | 1,040 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,873 IT Pros & Developers. It's quick & easy.

echo all the time vs echo at the end - what is faster

P: n/a
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.

Cheers
Nicolaas
Nov 4 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
windandwaves wrote:
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.

I think you've answered your own question as to which is better. Regards the
question of speed - why don't you measure it. I suspect you'll find the
latter is faster because it won't use chunked encoding - particularly if
you are using compression.

A better way of buffering the output is to use PHP's output buffering
functions.

C. Cheers
Nicolaas


Nov 4 '05 #2

P: n/a
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 - ju****@koivi.com
http://koivi.com
Nov 4 '05 #3

P: n/a
A recent survey shows that 4 out of 5 dentists prefer the first method.

Nov 4 '05 #4

P: n/a
Justin Koivisto wrote:

....
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.


Hi Justin,

Thank you for your reply. You are right, the speed of the script is not
really the problem. I was more thinking about the connection between server
and client. That is, is it faster to send one chunk a few "seconds" later
OR does it make more sense to send small chunks immediately OR are all the
small chunks congregated anyway on the way to the client and so it makes
absolutely no difference?

I hear what you saying about adding to a long string, that can take some
time.

Testing is not really an option, it would take too long and I work on so
many different servers that it could become a complete project in itself ;-)

Thanks again

- Nicolaas
Nov 4 '05 #5

P: n/a
Colin McKinnon wrote:
....
A better way of buffering the output is to use PHP's output buffering
functions.


but that means that I can not manipulate the output anymore? right?
Nov 4 '05 #6

P: n/a
In article <mr*****************@news.xtra.co.nz>,
"windandwaves" <wi*********@coldmail.com> 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.


<?
ob_start();
?>
HTML Code and stuff
<?
$output = ob_get_clean();
print $output;
?>

I.e. no need to rewrite all your stuff.
--
Sandman[.net]
Nov 6 '05 #7

P: n/a
windandwaves (wi*********@coldmail.com) wrote:
: Colin McKinnon wrote:
: ...
: > A better way of buffering the output is to use PHP's output buffering
: > functions.

: but that means that I can not manipulate the output anymore? right?

Wrong.

After doing the buffering, one of the ob_ functions can return the output
to your program as a string, which you can then manipulate to your hearts
content.
--

This programmer available for rent.
Nov 6 '05 #8

P: n/a
windandwaves wrote:
Hi Folk

My question is:
echo all the time vs echo at the end - what is faster

<snip>

As everyone stated, comparing the two, (1) is faster. But, none of
them are faster as compared to embedded HTML/PHP.
<quote src="http://in2.php.net/language.basic-syntax#AEN2666">
for outputting large blocks of text, dropping out of PHP parsing mode
is generally more efficient than sending all of the text through echo()
or print().
</quote>
--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Nov 7 '05 #9

P: n/a
R. Rajesh Jeba Anbiah wrote:
windandwaves wrote:
Hi Folk

My question is:
echo all the time vs echo at the end - what is faster

<snip>

As everyone stated, comparing the two, (1) is faster. But, none of
them are faster as compared to embedded HTML/PHP.
<quote src="http://in2.php.net/language.basic-syntax#AEN2666">
for outputting large blocks of text, dropping out of PHP parsing mode
is generally more efficient than sending all of the text through
echo() or print().
</quote>


Agree. While speed is important to me, efficient code is probably even more
important to me.

I now use the "ob_start and flush" method to clean up all my html (it
becomes one line without any tabs, double spaces) for speed, while keeping
the code nicely embedded in php (much easier to put in variables without the
labourious <?php echo $my_variable; ?> every time you want to do so).

Thanks

- Nicolaas
Nov 7 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.