php

PHP ob_start() Function

The ob_start() is a built-in function of PHP to enable the output buffering. If the output buffering is enabled, then all output will be stored in the internal buffer and no output from the script will be sent to the browser. Some other built-in functions are used with ob_start() function. The ob_get_content() function is used to store the content of the internal buffer into a variable.

The ob_end_flush() function is used to print the content of the internal buffer. The ob_end_clean() function is used to clean the content of the internal buffer. The uses of the ob_start() function in PHP has shown in this tutorial.

Syntax:

All arguments of ob_start() functions are optional and it can take three arguments. The syntax of this function is given below.

bool ob_start(callable $callback = null, int $chunk_size = 0, int $flags = PHP_OUTPUT_HANDLER_STDFLAGS)
  • The first optional argument takes a callback function name to modify the content of the buffer before sends to the output.
  • The second optional value is used to set the buffer size and the default value of this argument is 0.
  • The third optional argument is used to set the bitmask that defines which operations are permitted and which are not permitted. The default value of this argument is PHP_OUTPUT_HANDLER_STDFLAGS that indicates that ob_flush(), ob_clean(), ob_get_flush(), ob_end_flush() and ob_end_clean() functions are permitted.
  • It returns true on success and returns false on failure.

Different Uses of ob_start() Function

The uses of the ob_start() function with other buffer-related functions have been explained in this part of the tutorial.

Example-1: Use of ob_start() function without any argument

Create a PHP file with the following script to know the use of the ob_start() function without any argument. Two string values have been sent to the buffer after calling the ob_start() function. The ob_end_clean() function has been called in the script to clean the buffer after calling the ob_start() function the first time. For this, the first string will be removed from the buffer and the second string will be printed in the output.

<?php
    //Call ob_start() function first time
    ob_start();
    //Print the string value
    echo "Testing string value1<br/>";
    //Clean the buffer
    ob_end_clean();

    //Call ob_start() function second time
    ob_start();
    //Print the string value
    echo "Testing string value2<br/>";
?>

Output:

The following output will be appeared after executing the above script.

Example-2: Use of ob_start() function with callback function

Create a PHP file with the following script to know the use of the ob_start() function with a callback function. A string variable is declared in the script. The callback() function has been declared to replace the particular portion of the string variable before flushing the output from the buffer. The ob_start() function is called with the first argument that will call the callback() function and return the modified string to the buffer. Next, the ob_end_flush() function has used to flush the content of the buffer into the browser.

<?php

//Declare a string variable
$str = "I like PHP programming.<br/>";
echo "<b>The original string:</b> $str";

//Define the callback function
function callback($buffer)
{
  //Replace the word 'PHP' with 'Python'
  return (str_replace("PHP", "Python", $buffer));
}

echo "<b>The replaced string:</b>";
//call the ob_start() function with callback function
ob_start("callback");

echo $str;
//Print content of the internal buffer
ob_end_flush();

?>

Output:

The following output will be appeared after executing the above script. According to the output, the word, ‘PHP’ from the string has been replaced by the word, ‘Python’. The string, ‘I like PHP programming’ has converted into the string, ‘I like Python programming’ by the callback() function.

Example-3: Use of ob_start() function with the bitmask value

It is mentioned before that the default bitmask value of the ob_start() function is PHP_OUTPUT_HANDLER_STDFLAGS. This function has many other bitmask values.

The uses of PHP_OUTPUT_HANDLER_CLEANABLE and PHP_OUTPUT_HANDLER_FLUSHABLE bitmasks have shown in this example.

No callback function has been defined for the ob_start() function here and the size of the buffer has been set to 0. The ob_clean() function will remove the content from the buffer. So the first output will not be printed in the browser.

<?php

    //Declare function with PHP_OUTPUT_HANDLER_CLEANABLE flag
    ob_start(null, 0, PHP_OUTPUT_HANDLER_CLEANABLE);
    echo "Linux Hint<br/>";

    //Clean the buffer
    ob_clean();

    //Declare function with PHP_OUTPUT_HANDLER_FLUSHABLE flag
    ob_start(null, 0, PHP_OUTPUT_HANDLER_FLUSHABLE);
    echo "Welcome to LinuxHint";


?>

Output:

The following output will be appeared after executing the above script. According to the output, the second output from the buffer has been printed.

Example-4: Use of ob_start() function with ob_get_contents() function

Create a PHP file with the following script to know the use of ob_start() function with a callback function and the ob_get_contents() function. The convert_upper() function has used as the callback function of the ob_start() function. The purpose of this function is to convert the content of the buffer into uppercase. Three string values have been sent to the buffer and the output of the ob_get_contents() have been stored into three variables. When the ob_end_flush() function will be executed then the modified content of the buffer will be printed in the browser. The var_dump() function will dumb that content of three variables.

<?php

//Define the callback function
function convert_upper($buffer)
{
  //Covert the string into uppercase
  return (strtoupper($buffer));
}

//Call on_start() with callback function
ob_start('convert_upper');

//The string will be converted into uppercase
echo "Adnan ";
$var1 = ob_get_contents();

echo "Sakib ";
$var2 = ob_get_contents();

echo "Mostafizur ";
$var3 = ob_get_contents();

//Print the converted string
ob_end_flush();
echo "<br/>";

//Print the dump values of the variable
var_dump($var1, $var2, $var3);
?>

Output:

The following output will be appeared after executing the above script. According to the output, three string values have converted into uppercase letters and the content of the three variables has not converted into the uppercase letter.

Conclusion

The ob_start() function is used in the script when it is required to store the data into the buffer for any particular purpose before printing into the output. The use of the ob_start() function with other related functions has been explained in this tutorial with multiple examples for helping the PHP users to know the use of this function.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.