Home >Backend Development >PHP Tutorial >PHP Output Buffering

PHP Output Buffering

WBOY
WBOYOriginal
2024-08-29 13:07:06595browse

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 Tests

Start 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
?>

How Output Buffering works in PHP?

Output buffering in PHP has lots of significance in terms of its working which will are as follows :

  • As PHP is an interpreted language it becomes difficult for the page directed by the output stream to get displayed easily. Thus, some initiatives are being taken like output buffering.
  • Output buffering helps in storing the data into some variable which will be used for rendering before the request is sent to the browser after the execution of the PHP script.
  • All the pages which get distorted and slow while loading gets proper if the page is being included with the output buffering and its various functions.
  • There are a lot of advantages associated with the output buffering in PHP one of which is developers make use of this PHP function religiously because of the fact that it decreases the number of interaction between the client and the server as a whole where HTML is sent to the browser at once thus making it more versatile, flexible and efficient for the bigger size projects where a lot of pages and components come into the picture as a big screen.
  • On the other hand, there are some more advantages, since the entire output buffer is stored in whole as a string in HTML using many other variables all the HTML files get manipulated and modified with the string method and other inbuild customized methods written by the programmers which in turn helps in the smooth rendering of the web pages in PHP.
  • Many other compression methods can also be used for creating and manipulating a much easier way to render.
  • Cookies and session management also play a pivotal role with respect to output buffering in PHP as it gives an added advantage for output buffering in PHP to work efficiently for getting header information which is sent as part of the content not entirely but whatever is required to be sent.
  • Also, it should be kept in mind that before using the output buffering in any PHP oriented application it is very much needed to check the compatibility issues related to PHP versions as it may use other PHP versions as per requirement then there might be a chance of PHP output buffering function may not work properly as desired. Thus, it is required to check whether the output buffering is enabled or not by default it is off.
  • It also provides some more abilities in terms of database calling as it allows the programmers to use some of the advanced functionalities like minimization and reduction which is appropriate for cookies and sessions as well.
  • Output buffering is considered one of the safest and efficient approach with extra care and ability towards the page rendering as it provides faster, flexible, smoother, secured approaches which are expected by most of the end-users.
  • It is one of the most modernized concepts to improvise the process of entire page navigation and rendering using buffering by holding data and various manipulation.

Examples

Let us discuss examples of PHP Output Buffering.

Example #1

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:

PHP Output Buffering

Example #2

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:

PHP Output Buffering

Example #3

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:

PHP Output Buffering

Example #4

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:

PHP Output Buffering

Example #5

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:

PHP Output Buffering

Example #6

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'));
?>

Output:

PHP Output Buffering

Conclusion

PHP output buffering is an efficient way of giving an output to the end-user by keeping the data into a buffer before putting it to the browser it keeps the data on hold and then it assigns a variable to make the reference as it gives programmers the ability to change and manipulate it accordingly for the end-user with proper requirement.

The above is the detailed content of PHP Output Buffering. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP File HandlingNext article:PHP File Handling