EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

PHP ob_start()

Home » Software Development » Software Development Tutorials » PHP Tutorial » PHP ob_start()

PHP ob_start()

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();

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The ob_start() function of PHP Programming Language accepts many bunch of some optional parameters. They are:

  1. Callback Function
  2. Chunk Size
  3. 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.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,726 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

How ob_start() Works 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 the 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:

PHP ob_start() Example 1

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:

PHP ob_start() Example 2

Example #3

This is the 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 “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:

PHP ob_start() Example 3

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 ob_start() better and so easily.

Recommended Article

This is a guide to the PHP ob_start(). Here we discuss the Introduction to PHP ob_start() Function and its Syntax & Parameters along with code Implementation. You can also go through our other suggested articles to learn more –

  1. Overview of Abstract Class in Python
  2. What is Abstract Class in PHP?
  3. Socket Programming in PHP with Methods
  4. PHP chop() | How to Work?

PHP Training (5 Courses, 3 Project)

5 Online Courses

3 Hands-on Project

28+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training (5 Courses, 3 Project) Learn More