473,405 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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
9 2031
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
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
A recent survey shows that 4 out of 5 dentists prefer the first method.

Nov 4 '05 #4
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: MM | last post by:
Hi, I don't know PHP..i use dreamweaver and now i realise it is far from enough. This code selects from a previous page where the users entered some votes and select the 7 products they vote the...
6
by: Marco | last post by:
I have a couple pages that have tables, whats best use echo to produce the full table with the variables or is it best to just make the table in plain html and use <?php echo ('$va'l); ?> when i...
6
by: James Smith | last post by:
What's the difference between Echo and Print? J.
13
by: Tony | last post by:
I'm just wondering why it seems most people use "echo" instead of "print". I tend to use "print", probably because I started programming in BASIC back in 78, so it's just familiar. Echo can take...
29
by: Roy Gourgi | last post by:
Hi, I am new to C#. I have the same time scheduling program written in C++ and it is 5 times faster than my version in C#. Why is it so slow as I thought that C# was only a little slower than...
2
by: L. Berger | last post by:
Hi, I am working on an HTML template which has a lot of html tags, with PHP data shown in the middle of these tags -- you know, the usual. Currently, I have HTML as is, and many many "echo...
3
by: Luca Cioria | last post by:
I know it may seem odd, but is it possible to echo to php? I mean neasting php within php? example <?php $array_levels=""; $my_array<?php echo $array_levels; ?>="hello world"
11
by: Twayne | last post by:
Hi, Irrelevant question time, probably: Is there any advantage/reason to use one format over the other for the following two types of echo statements? 1. echo "error is " . $error; 2. ...
32
by: Request-1 | last post by:
hi folks, html coder here, new and terrified * of php!! aaaaaa! i'm trying to bury a JS script to rotate a photo, in a page i converted from html to php. the conversion went well, it was to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.