i use ob_start() for quick fix to my stupid header already sent problem.
but what is the real benefit of using output buffering?
ob_start();
ob_end_flush();
i use ob_start() for quick fix to my stupid header already sent problem.
but what is the real benefit of using output buffering?
ob_start();
ob_end_flush();
Can any one share example of ob_Start() in the context of template? Say, I wish to pass partial content to my view from controller…
On the first request when the script is doing its normal work and object buffering it will use more memory. On the subsequent requests, when it’s finds a pre-rendered cache file on the file system and avoids having to do its normal work it will do less. The heavier the work that gets cached the bigger the saving.
is it true ob_start() uses less memory resources but take up more HDD space?
i though ‘buffer’ means storing into memory, ‘save’ is save to HDD space.
are u sure by using ob_start() on top of my script will use less memory?
if yes, perhaps i should remove ob_end_flush() at bottom of my script so to keep buffering and caching in HDD.
It depends what you mean by resources - it requires more disk space, but requires less memory over all.
Put it this way; if you have a page that grabs 30000 rows from a database table and displays them all in a nice format, its going to take a fair bit of processing. Normally pagination is a nice workaround, but assume that it isn’t a viable solution here.
Now, you can either run that query every time someone visits the page, or you could cache that page. One file request probably takes less time than grabbing and processing 3e4 rows from a database.
My preferred use of output buffering is templating. If I want to have a template, it’s going to be as simple to modify as possible. If every line is assigning to a variable, it will look a complete mess. By using echo, it won’t.
An interesting use of output buffering is when you have an output which you want to transform in some way. For example, if you (for whatever reason) wanted to remove all HTML tags in your output, you could run your application as normal but write the output to a variable, then pass it through striptags(). It means you don’t have to go round modifying all of your template files for what could be a temporary situation!
at the expense of more server resources?
I’ll add another voice to the chorus. It’s all about WHAT resources you care about. If the resources you’re running low on is server memory, then yes, this is going to be expensive and it may not be the right idea for you.
However, if you want to improve the speed with which your application runs (increase performance) and / or you’re taxing your processor speed (ie decrease processor load) then this is a really powerful way to increase the speed of your app and decrease the server resources you need. Output caching is a way of allowing the server memory to essentially store critical components of work that you don’t want to have to process again. Perhaps even more importantly, for applications which aren’t maxed out, caching done properly has major performance benefits.
If you’re including templates, then ob allows you to grab the buffer to a variable for processing or passing on to others. If not you have to use return in the template.
That is the only possibe benefit it has since for anything other than that you could just add all of the content to a variable and then echo the variable at the end.
Caching. Start buffering at the beginning, and you can capture the rendered page and cache it for future requests.
try-catch, is the first thing that comes to my mind.
Form the output, and if you hit an error, abort the buffer and output something different.