Updated April 15, 2023
Introduction to PHP ob_start() Function
The ob_start() function of the PHP Programming Language helps in enabling the Buffering of the specific output before any type of echoing and any HTML in the specific mentioned script. We all know that PHP is one of the interpreted web developing programming languages so each and every statement in the program will be executed one after the other. So the PHP helps in sending the HTML to the web browsers in some chunks thus helps in reducing the performance. With the help of the output buffering the HTML which is generated will be stored in buffer after the last PHP script execution. To overcome this ob_start() of PHP came into existence.
Syntax & Parameters of PHP ob_start()
ob_start();
The ob_start() function of PHP Programming Language accepts many bunches of optional parameters. They are:
- Callback Function
- Chunk Size
- Flags.
1. Callback Function: The callback function parameter help in expecting a function that usually takes the output buffer’s content and then returns a string that is to be sent specifically to the browser for the rendering. The callback function parameter normally used for compressing HTML Content. It is an optional parameter of ob_start() function.
2. Chunk Size: The chunk size parameter of the ob_start() function is also an another optional parameter and helps in setting the output buffer size and then outputs if the buffer is either exceeded or full.
3. Flags: The flags parameter of the ob_start() function of the PHP Programming language helps in accepting bitmask in order to control some operations which will be implemented on some specific output buffer. The flags parameter helps in passing the restricted access and the default permissions helps in giving access to clean and buffer removal. This parameter is also an optional parameter just like the other two parameters.
Return Type of the ob_start() function of PHP:
The ob_start() function helps in returning the TRUE value on success output or else you will get False as an output return.
How does ob_start() work in PHP?
The ob_start() of the PHP Programming Language helps in enabling the Output Buffer/Buffering before any type of echoing in any type of some HTML content in the PHP script. The ob_start() function don’t accepts any parameter specifically but it works by accepting some optional parameters. They are : callback parameter, Chunk Size parameter and Flags Parameter. This ob_start() only works on PHP 4, PHP 5 and PHP 7 versions. It only works just by turning on the output buffering.
Examples to Implement PHP ob_start() Function
Below are the Examples of PHP ob_start():
Example #1
This is an example of illustrating the ob_start() function of the PHP Programming Language in order to understand the callback functionality of the ob_start() function. Here at first PHP tags are opened and then a function with a parameter is created. Then inside of the function return function is used with strtoupper() function to return the output in capital letters. Then ob_start() function is used with the callback parameter which helps in changing the output. Here the output is a string that is mentioned with the help of the echo statement. Here the string is “Hello Educba!!” and this will be changed into an upper case like “HELLO EDUCBA!!”. Check out the output so that you will understand what is happening in the syntax.
Code:
<?php
// This is PHP code which helps in illustrating the working
// of the ob_start() Function of PHP Language
function callback($buffer1){
// This function Returns Everything of output in CAPS.
return (strtoupper($buffer1));
}
ob_start("callback");
echo "Hello Educba!!";
?>
Output:
Example #2
This is also an example of illustrating the ob_start() function of the PHP Programming Language which helps in handling the output buffering. Here at first, inside of the PHP tags, a function called callback is created with the buffer1 as a parameter. Inside of the function str_replace() function is used which helps in returning the output of the output text just by replacing the required string text according to the need. Here mangoes and Pomegranates and the mangoes text will be replaced by the “Pomegranates” text. Then the function parenthesis are closed. Then ob_start() function is used with the callback parameter for the required return output. Then HTML tags are used. Inside the HTML and BODY tags, some string text is used. The string text can be a string or some paragraph that is actually mentioned based on our requirement. Her in the following text, the string text “mangoes” will be replaced with “Pomegranates”.
Code:
<?php
function callback($buffer1)
{
// This will help in replacing all Mangoes with the Pomegranates
return (str_replace("mangoes", "Pomegranates", $buffer1));
}
ob_start("callback");
?>
<html>
<body>
<p>It's like comparing the mangoes to Pomegranates.</p>
</body>
</html>
<?php
echo "<br>";
ob_end_flush();
?>
Output:
Example #3
This is an example of illustrating the ob_start() of the PHP Programming Language. Here at the first inside of the PHP tags, If the condition is created and then inside of it a function is mentioned as the condition and then the ob_start() function is used along with the callback parameter, chunk size parameter along with the flag parameters. If the IF condition is TRUE then the “Your PHP version is greater than or equal to PHP 5.4.0 version“ string text will be printed and if the IF condition is FALSE value then else condition statements will be printed. In ELSE also we used the ob_start() function is used with callback value as NULL, Chunk size parameter value as 0 and FALSE as the FLAG value. So this doesn’t produce any output. So to recognize this we used some string text with ECHO is used. PHP_OUTPUT_HANDLER_REMOVABLE is used to remove the output which is created by ob_start() just before the end of the script. PHP_OUTPUT_HANDLER_STDFLAG is the default set of some output buffer flags and it is equivalent to PHP_OUTPUT_HANDLER_REMOVABLE.
Code:
<?php
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
ob_start(null, 0, PHP_OUTPUT_HANDLER_STDFLAGS ^
PHP_OUTPUT_HANDLER_REMOVABLE);
echo "Your PHP version is greater than or equal to PHP 5.4.0 version";
} else {
ob_start(null, 0, false);
echo "Your PHP Version is less than PHP 5.4.0 version";
}
?>
Output:
Conclusion
I hope you learned what is the definition of ob_start of the PHP Programming Language along with its syntax and explanations, How the ob_start() function works in PHP along with various examples of ob_start() function to understand the ob_start() better and so easily.
Recommended Article
This is a guide to the PHP ob_start(). Here we discuss the introduction, syntax, and working of the ob_start() function in PHP along with different examples and code implementation. You can also go through our other suggested articles to learn more –