Home >Backend Development >PHP Tutorial >PHP Output Buffering
PHP output buffering is a process of acknowledging the PHP engine to hold the data while providing output whenever input is fed for processing. Once the PHP engine gets a processed data for execution to provide an output then simultaneously that data is sent to the engine in bits and pieces to the browser. If output buffering mechanism as mentioned is used for execution, then that will give more efficiency and feasibility in terms of data processing because data first gets stored in the variable, and then it is sent to the browser as part of the script.
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:
There is no fixed format for output buffering, but it can be represented and used in the following way :
<?php function to start php_info( ); // processing before giving the output. use variable to assign the final value as an output ?>
Output buffering in PHP has lots of significance in terms of its working which will are as follows :
Let us discuss examples of PHP Output Buffering.
This program demonstrates the callback() function which is defined by the user which will replace the value as defined within the variable as shown in the output.
Code:
<!DOCTYPE html> <html> <body> <?php function cll_bck($buff) { return (str_replace("Mobile", "Tabs", $buff)); } ob_start("cll_bck"); ?> <html> <body> <p>Everyone_prefers_Mobile_over_Tabs.</p> </body> </html> <?php ob_end_flush(); ?> </body> </html>
Output:
This program demonstrates the ob_get_contents() function to get the content defined to the final engine while passing the variable as shown in the output.
Code:
<!DOCTYPE html> <html> <body> <?php ob_start(); echo "Today_day_is_good. "; $o_t_1 = ob_get_contents(); echo "and_pleasant"; $o_t_2 = ob_get_contents(); ob_end_clean(); var_dump($o_t_1, $o_t_2); ?> </body> </html>
Output:
This program demonstrates the ob_start function where the output buffering gets initiated and then it gets displayed as shown in the output.
Code:
<!DOCTYPE html> <html> <body> <?php ob_start(); echo 'Text written will_get displayed easily.'; ?> </body> </html>
Output:
This program demonstrates the use of text that will get removed once the ob_end_clean function is called as shown in the output.
Code:
<!DOCTYPE html> <html> <body> <?php ob_start(); echo 'Text_written_will_get_removed_easily_using ob_end_clean.'; ob_end_clean(); ?> </body> </html>
Output:
This program demonstrates the ob_list_handlers() function which is used to return an array with the output buffer handler with the list of handlers as shown in the output.
Code:
<!DOCTYPE html> <html> <body> <?php print_r(ob_list_handlers()); ob_end_flush(); ob_start("ob_gz_handler"); print_r(ob_list_handlers()); ob_end_flush(); ob_start(function($str_2) { return $str_2; }); print_r(ob_list_handlers()); ob_end_flush(); ?> </body> </html>
Output:
This program demonstrates the encoding and decoding of all types of possible codes being defined but if in case something is missing, or the browser is getting some value as wrong then it will return the output as shown.
Code:
<!DOCTYPE html> <html> <body> <pre class="brush:php;toolbar:false"> <?php iconv_set_encoding("int_encd", "internal_UTF_8"); iconv_set_encoding("o/p_encd", "ISO-8859-1"); var_dump(iconv_get_encoding('all_encd_types')); ?>