Home » PHP ob_start() Function

PHP ob_start() Function

by Online Tutorials Library

PHP ob_start() Function

Ob _ start function is used to create an output buffer in PHP, as we are already aware that PHP is an interpreted language, i.e. any program written in PHP will be executed stepwise, one statement after another, which makes processing comparatively slow compared to others.

Therefore, PHP uses the ob_start() function, which creates an output buffering that stores generate HTML inside a cluster/buffer or a string variable that is later sent to render, thus increasing speed and reducing execution time.

In order to execute the output buffer, we have to use the ob_start () function, which uses a callback function that is used to process the contents of the buffer.

The function will return TRUE on successful transmission and FALSE on failure

Syntax:

S.No Parameter Description Mandatory / optional
1 Call back The main task of call back is to process all the buffer content before the content is flushed out, i.e., it takes all the content from the output buffer and returns the content as a string sent to the browser for rendering.
Also, call back itself holds two more parameters (buffer, phase)
Buffer: The content of the output buffer
Phase: flags used
optional
2 Chunk size This parameter is used to set the size of the output buffer, and it will automatically execute the output as the buffer reaches the desired chunk size optional
3 flag This parameter uses a bitmask that controls all the operations that will be executed on the output buffer. This parameter is used to restrict the access and permission granted to the buffer.
The default buffer flag is
PHP _ OUTPUT _ HANDLER _ STDFLAGS : this flag performs the work of 3 flag to clean, flush and remove buffer
PHP _ OUTPUT _ HANDLER _ CLEANABLE – to clean the buffer it contain ob_clean(), ob_end_clean() and ob_get_clean()
PHP _ OUTPUT _ HANDLER _ FLUSHABLE – to flush the buffer it contains ob_flush(), ob_end_flush() and ob_get_flush()
PHP _ OUTPUT _ HANDLER _ REMOVABLE – to remove the buffer it contains ob_end_clean(), ob_end_flush() and ob_get_flush()
optional

Example 1:

Output:

PHP ob_start() Function

Here in this program, we have used OB _ START ( ) function to create the output buffer, and the OB _ END _ CLEAN ( ) function will be used to clear all the data of the created buffer; therefore, only the text outside the B _ START ( ) function and OB _ END _ CLEAN ( ) function will be displayed.

Example 2:

Output:

PHP ob_start() Function

here in this program, first, we have declared the CALL BACK ( ) function with variable $firstbuffer. The property of this function is to return everything written inside as capital. We have used OB _ START ( ) function to create an output buffer with parameters as call back to initiate the function. Due to the call function, all the text after OB _ START ( ) will be displayed in caps.

Example 3:

Output:

PHP ob_start() Function

As we know, a buffer stores the data in a string variable. We can use all the properties of strigs. Here in this program, first, we have declared the CALL BACK ( ) function with variable $mybuffer. The property of this function is to replace a declared string with another. We have used the OB _ START ( ) function to create an output buffer with the parameter call back to initiate the function. Due to the call function, all the b after OB _ START ( ) will be converted to * * *.


Next TopicPHP Beautifier

You may also like