Is using output buffering considered a bad practice? [closed]
Asked Answered
T

8

30

Are ob_start / ob_get_clean() considered bad practice by php programmers in general?

Are there any disadvantages of output buffering?

Trait answered 19/1, 2011 at 3:8 Comment(0)
P
21

It's actually a good practice. Speed up data transfer

Plier answered 19/1, 2011 at 3:17 Comment(3)
Precisely, how does it do that? How does this answer actually help me?Homoeo
He may be referring to flushing the output buffer just after the </head> tag. See developer.yahoo.com/performance/rules.htmlMarden
@Tim PHP processes all the output first before displaying it to the client. So rather then load each little bit of output at a time it does it entirely at once, after it has processed.Incantation
F
15

Output buffering in some circumstances is almost mandatory. With PHP as soon as you output something back to the user, headers are sent. Therefore if you get partway through processing a page and something happens that requires a header being sent you cant unless buffering is turned on. Otherwise you get the dreaded "Cannot modify header information – headers already sent".

Some will tell you you shouldn't code that way. humbug I say!

With buffers turned on your code can be more flexible.

Fourdrinier answered 19/1, 2011 at 3:29 Comment(0)
B
10

output buffering is NOT a bad practice. For example it can speed up the loading of your website by using GZIP compression(although if possible it is better to do this inside .htaccess).

<?php 
    if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) 
        ob_start("ob_gzhandler"); 
    else 
        ob_start(); 
?>

Disadvantages: I don't know. Good question.

P.S: also I found this topic about output buffering.

Brokendown answered 19/1, 2011 at 3:16 Comment(2)
actually for some browsers you'd better send header( 'Content-Length: '. ob_get_length() ); after sending text, otherwise they may not decode it right. For some ancient browsers, but there is a percent of them stillPlier
I guess you could be right. Thanks for the information :)Brokendown
A
7

For program efficiency, output buffer capturing is not bad. It is a useful feature of PHP. However, it can be used for things that are better done with other methods. Code smell can be an issue with it, so make sure that there's not a better, clearer solution to your problem.

Anglicism answered 19/1, 2011 at 3:20 Comment(1)
Problems with one's implementation that cause other (sometimes larger) problems. In this case using the wrong solution to solve a problem. More here: codinghorror.com/blog/2006/05/code-smells.htmlAnglicism
B
4

It's not considered bad (or good). Some people like it, some don't Personally I think there are reasons not to use it. I think of it as a last resort. Sometimes you may have situation where output buffering may be your only choice to solve a particular problem, so save this option for just such situations.

I don't think there is any performance gain or speeding up of page load by using it, but it also depends on what server you using and if you using php as mod_php or as cgi or fastcgi.

Bladder answered 19/1, 2011 at 3:33 Comment(0)
G
3

The main disadvantage of output buffering is not knowing (or paying attention to) how deep your buffer stack is. Combine this with overly aggressive error handling or subroutines that exit/die unexpectedly and you will lose whatever is in the buffer, leaving few clues as to what is going on.

For example, the Zend framework makes use of output buffering for almost everything, but when it hits a critical error it prints a message and exits immediately. Any helpful debugging information is lost.

Griqua answered 19/1, 2011 at 5:33 Comment(0)
M
1

If I'm not mistaken java also have this input and output buffering to read and write file.

Metric answered 19/1, 2011 at 3:10 Comment(0)
F
1

Surely output buffering means that content which could have been sent to the browser immediately is now sticking around on the server, which is taking up extra memory (a very important issue if you're dealing with high scalability) so if your program is then taking a while to execute, this memory overhead would impair performance.

I don't know PHP well enough to say if this is true or if it even frees the memory when you don't use buffering, but that is usually the theory.

Fluor answered 17/5, 2011 at 13:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.