Let’s take a quick recap. PHP is an interpreted language thus each statement is executed one after another, therefore PHP tends to send HTML to browsers in chunks thus reducing performance. Using output buffering the generated HTML gets stored in a buffer or a string variable and is sent to the buffer to render after the execution of the last statement in the PHP script.
But Output Buffering is not enabled by default. In order to enable the Output Buffering one must use the ob_start() function before any echoing any HTML content in a script.
bool ob_start ()
Parameters: The function can accept a bunch of optional parameters as follows:
- Callback function: This is an optional parameter that expects a function that takes the contents of the output buffer and returns a string that is to be sent to the browser for rendering. The callback function is generally used for compressing the HTML content.
- Chunk size: This is another optional parameter that sets the output buffer size of provided size and outputs as soon as the buffer is either full or exceeded.
- Flags: This is another optional parameter that accepts a bitmask to control the operations that can be implemented on the output buffer. This parameter is passed to restrict access. The Default permissions gives access to clean, flush and removal of the buffer.
Return Type: This function returns TRUE on success otherwise FALSE.
Below program illustrates the working of ob_start() in PHP:
Important points to note:
- Enables Output Buffering.
- Output Buffering flags can be of four types PHP_OUTPUT_HANDLER_CLEANABLE(only clean), PHP_OUTPUT_HANDLER_FLUSHABLE(only flush), PHP_OUTPUT_HANDLER_REMOVABLE(only remove), PHP_OUTPUT_HANDLER_STDFLAGS(allowed every operation).
- Output buffers are stackable, thus nested ob_start() methods are allowed and works as desired if they are closed/flushed sequentially.
- How to get the function name inside a function in PHP ?
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- PHP | imagecreatetruecolor() Function
- PHP | fpassthru( ) Function
- PHP | ImagickDraw getTextAlignment() Function
- PHP | Ds\Sequence last() Function
- PHP | Imagick floodFillPaintImage() Function
- Function to escape regex patterns before applied in PHP
- PHP | array_udiff_uassoc() Function
- PHP | geoip_continent_code_by_name() Function
- PHP | GmagickPixel setcolor() function
- PHP | opendir() Function
- PHP | cal_to_jd() Function
- PHP | stream_get_transports() Function
- PHP | Ds\Deque pop() Function
- PHP | SimpleXMLElement children() Function
- PHP | array_intersect_ukey() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.