Open In App

PHP | ob_start() Function

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.



Syntax:

bool ob_start ()

Parameters: The function can accept a bunch of optional parameters as follows:



Return Type: This function returns TRUE on success otherwise FALSE.

Below program illustrates the working of ob_start() in PHP:




<?php
  
// PHP code to illustrate the working
//  of ob_start() Function 
  
function callback($buffer){
    // Return Everything in CAPS.
    return (strtoupper($buffer));
}
  
ob_start("callback");
echo "Hello Geek!";
ob_end_flush();
  
?>

Output:

HELLO GEEK!

Important points to note:

Reference:
http://php.net/manual/en/function.ob-start.php

Article Tags :