Question

I know this question has been asked before but I haven't been able to find a definitive answer.

Does the overly use of the echo statement slow down end user load times?

By having more echo statements in the file the file size increases so I know this would be a factor. Correct me if I'm wrong.

I know after some research that using php's ob_start() function along with upping Apaches SendBufferSize can help decrease load times, but from what I understand this is more of decrease in php execution time by allowing php to finish/exit sooner, which in turn allows Apache to exit sooner.

With that being said, php does exit sooner, but does that mean php actually took less time to execute and in turn speed things up on the end user side ?

To be clear, what I mean by this is if I had 2 files, same content, and one made use of the echo statement for every html tag and the other file used the standard method of breaking in and out of php, aside for the difference in file size from the "overly" use of the echo statement (within reason I'm guessing?), which one would be faster? Or would there really not be any difference?

Maybe I'm going about this or looking at this wrong?

Edit: I have done a bit of checking around and found a way to create a stop watch to check execution time of a script and seems to work quit well. If anybody is interested in doing the same here is the link to the method I have chosen to use for now.

http://www.phpjabbers.com/measuring-php-page-load-time-php17.html

Was it helpful?

Solution

Does the overly use of the echo statement slow down end user load times?

No.

By having more echo statements in the file the file size increases so I know this would be a factor. Correct me if I'm wrong.

You are wrong.

does that mean php actually took less time to execute and in turn speed things up on the end user side?

No.

Or would there really not be any difference?

Yes.

Maybe I'm going about this or looking at this wrong?

Definitely.

There is a common problem with performance related questions.
Most of them coming up not from the real needs but out of imagination.
While one have to solve only real problems, not imaginable ones.

OTHER TIPS

This is not an issue.

You are overthinking things.

This is an old question, but the problem with the logic presented here is it assumes that “More commands equals slower performance…” when—in terms of modern programming and modern systems—this is an utterly irrelevant issue. These concerns are only of concerns of someone who—for some reason—programs at an extremely low level in something like assembler and such,.

The reason why is there might be a slowdown… But nothing anyone would ever humanly be able to perceive. Such as a slowdown of such a small fraction of a second that the any effort you make to optimize that code would not result in anything worth anything.

That said, speed and performance should always be a concern when programming, but not in terms of how many of a command you use.

As someone who uses PHP with echo statements, I would recommend that you organize your code for readability. A pile of echo statements is simply hard to read and edit. Depending on your needs you should concatenate the contents of those echo statements into a string that you then echo later on.

Or—a nice technique I use—is to create an array of values I need to echo and then run echo implode('', $some_array); instead.

The benefit of an array over string concatenation is it’s naturally easier to understand that some_array[] = 'Hello!'; will be a new addition to that array where something like $some_string .= 'Hello!'; might seem simple but it might be confusing to debug when you have tons of concatenation happening.

But at the end of the day, clean code that is easy to read is more important to all involved than shaving fractions of a second off of a process. If you are a modern programmer, program with an eye towards readability as a first draft and then—if necessary—think about optimizing that code.

Do not worry about having 10 or 100 calls to echo. When optimizing these shouldn't be even take in consideration.

Think that on a normal server you can run an echo simple call faster than 1/100,000 part of a second.

Always worry about code readability and maintenance than those X extra echo calls.

Didn't made any benchmark. All I can say is, in fact when your echo strings (HTML or not) and use double quotes (") it's slower than single quotes (').

For strings with double quotes PHP has to parse those strings. You could know the possibility to get variables inside of strings by just insert them into your string:

echo "you're $age years old!";

PHP has to parse your string to lookup those variables and automatically replace them. When you're sure, you don't have any variables inside your string use single quotes.

Hope this would help you.

Even when you use a bunch of echo calls, I don't think it would slow down your loading time. Loading time depends on reaction time of your server and execution time. When your loading time would be to high for the given task, check the whole code not only the possibility of echoes could slow down your server. I think there would be something wrong inside your code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top