PHP ob_start()
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.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
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 –
- Overview of Abstract Class in Python
- What is Abstract Class in PHP?
- Socket Programming in PHP with Methods
- PHP chop() | How to Work?
The above is the detailed content of PHP ob_start(). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
