What is Output Buffering in PHP, It’s Advantage and Usage

What is Output Buffering

PHP sends data to your web browser as soon as it is ready – this might be line by line or in chunks. It makes your webpage slow and you have to maintain the order in which the data need to be sent. What Output buffering do is, It stores the data in buffer. It returns the data to the browser when buffer is flush. When buffer is flush then all the data is sent at once instead of chunks.

Buffer is flush when

1. Interpreter reach at the end of file.

2. When buffer size reach to it’s maximum limit.

3. When you called ob_end_fush() or ob_flush().

** The Output Control functions do not affect headers sent using header() or setCookie().

Advantage of Output Buffering

1. Decrease the download and rendering time – When output buffer is on, HTML is sent to the browser in one go instead of pieces. This decreases the download and rendering time.

Speed up Your Website through Memcache.

2. If your output buffer is on .You will never encountered Warning: Cannot modify header information – headers already sent .

3. You can pur or write the output content on file (make a cache) or variable using ob_get_contents().

By default the config of output_buffering is off in php.ini file. You can enable them by making output_buffering=on.

To use output buffering in code use ob_start().

NOTE-

1. When output buffering is on, no output is sent except headers. The output is stored in internal buffer.

2. Output buffers are stackable . It means you can call another ob_start while one is active.

To sent the content to the browser

ob_get_contents – It returns the content of the current buffer as string. But it doesn’t clear or stop the buffer.

ob_get_clean – It returns the buffer content and deletes it.

If you want to use buffer content

More read on Output Buffering

Tagged , , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.