top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Purpose of output buffering in PHP?

+3 votes
551 views
Purpose of output buffering in PHP?
posted Feb 6, 2016 by Vrije Mani Upadhyay

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

What is Output Buffering in PHP
HTML is sent to the browser in pieces as PHP processes through PHP script, but with output buffering, HTML is stored in a variable and sent to the browser as one piece at the end of your script.

Advantages of output buffering
Turning on output buffering decreases the amount of time it takes to download and render HTML because it's not being sent to the browser in pieces as PHP processes the HTML.

How to turn-on the buffering
Use ob_start and ob_end_clean/ob_end_flush/ob_clean/ob_flush as follows

ob_start();
 // Your HTML portion 
ob_end_clean();
answer Feb 6, 2016 by Salil Agrawal
Wonderful explanation +1 from me.
0 votes

PHP code is not multi-threaded, we can not do this if the processing is hung up doing one function so we need output buffering in php and other advantages:
PHP store all output into a buffer and output all of it at once improving network performance.
we can access the buffer content without sending it back to browser in certain situations.

answer Feb 6, 2016 by Shivam Kumar Pandey
Similar Questions
+2 votes

When I'm trying to load an external html document with the loadHTMLFile() function and then I use the saveHTML() function to output its content, some text in the external html document which is written in non english is displayed in a gibberish format.
Is anyone here has an idea how to solve this problem?

+3 votes

Say I have sent a command to a process using the fwrite command something like

...
while (true) {
 switch (expect_expectl($stream, $cases)) {
 case "ls":
 fwrite($stream, "ls -aln");
 break;
..

Is there a way I can save the output from that process to a variable ?

...