Are ob_start
/ ob_get_clean()
considered bad practice by php programmers in general?
Are there any disadvantages of output buffering?
Are ob_start
/ ob_get_clean()
considered bad practice by php programmers in general?
Are there any disadvantages of output buffering?
It's actually a good practice. Speed up data transfer
</head>
tag. See developer.yahoo.com/performance/rules.html –
Marden 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.
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.
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.
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.
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.
If I'm not mistaken java also have this input and output buffering to read and write file.
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.
© 2022 - 2024 — McMap. All rights reserved.