Web Development

PHP Output Buffering

Output buffering is a very Good feature of PHP. You can have control over your output using the output buffering. You can decide what to buffer and when to output to the browser.

What is Output Buffering?

When you execute any PHP script and generate an HTML content, that HTML content is server to the browser. But what if you want to generate and HTML content and don’t pass it to browser? Yes, here Output Buffering comes into picture.

Normally PHP don’t do it, but when you start Output Buffering PHP will keep all your output content in Internal Buffer rather than passing it to browser. PHP will pass this content only when user has requested to output the buffer content or script execution ends.

Default state for Output buffering is OFF.

How to Start Output Buffering?

We just need to call one PHP function to start the PHP Output Buffering. Have a look at below code block:

[cc lang=”php”]
ob_start();
[/cc]

How to send output buffer to browser?

Once output buffer is started nothing will be outputed to the browser untill you request to output or script execution ends. If you want to output all the content of the buffer to browser then you need to use below code block:

[cc lang=”php”]
ob_flush();
[/cc]

But the thing is that, [code]ob_flush();[/code] will output the content from the buffer but PHP script will continue to buffer further. So what if you want to output the buffered content and stops the buffering anymore? You will need below code block for the same:

[cc lang=”php”]
ob_end_flush();
[/cc]

How to delete Output Buffer?

Just like an outputing, deleting is also done by just one function call. If you want to delete all content from the buffer then you will need below code block:

[cc lang=”php”]
ob_clean();
[/cc]

But like ob_flush(), ob_clean(); will continue to buffer the content, So to delete the content and stops the buffering anymore you need to use below code:

[cc lang=”php”]
ob_end_clean();
[/cc]

How to get Output Buffer Content?

So now we have seen the functions for deleting and outputing the buffer values, but what If you just want to check the content which are stored in the PHP buffer? Simple you will need to use below code block for the same:

[cc lang=”php”]
$var = ob_get_content();
[/cc]

Working Example

Have a look at below code block. Here you can see that we are outputting the phpinfo() and then after we have used [code]sleep(10)[/code]. We have not used ob_start() here. So what happes, it will output the phpinfo and the it will wait for sleep to finish.

[cc lang=”php”]
phpinfo();
sleep(10);
[/cc]

Now in below code block you can see the magic of the output buffering, here you can see that we have used ob_start() and sleep() together. Execute below code in any php file and you can see that PHP script will wait for 10 seconds of sleep even if that is called after phpinfo().

[cc lang=”php”]
ob_start();
phpinfo();
sleep(10);
[/cc]

To see this in action better if you create two different PHP file and check it executing those files.

Benefits

One major benefit for Output beffering is caching. Yes you can cache the content (in fact whole rendered page) with the help of output beffering. Just to explain how this works. You can start output buffering and you can buffer whole content and at the end of the script before echoing the output you will have content of whole page, which you can cache in any ways you want.

Shares:
  • Mukesh
    Mukesh
    December 20, 2019 at 2:55 pm

    Dear Sir in both cases script takes same time and display output

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *