Home > Backend Development > PHP Tutorial > Controlling buffers under PHP_PHP tutorial

Controlling buffers under PHP_PHP tutorial

WBOY
Release: 2016-07-15 13:23:06
Original
911 people have browsed it

PHP4.0 provides a collection of output buffering functions. Output buffering support allows you to write functions that wrap functions around compressed buffers. Output buffering support in PHP4 allows HTML header information to be stored regardless of whether the HTML body is output. But in PHP, header information ((header(), content type, and cookies) does not use buffering.
In the process of using PHP, you must use the header and

setcookie functions. These two functions A piece of file header information will be sent to the browser, but if there is any output (including empty output, such as spaces, carriage returns and line feeds) before using these two functions, an error will be prompted with the following message: "Header had all ready send by”!. Several functions for buffer control have been added to PHP 4.0. Using these functions can help us solve many problems.
 
Function name Function format Function description
Flush flush() output Contents in the buffer and delete the buffer. This function is often used and is very efficient.
ob_start void ob_start(void) When the buffer is activated, all non-file header information from the PHP program is deleted. will be sent, but stored in the internal buffer. In order to output the contents of the buffer, you can use ob_end_flush() or use ob_end_clean() to output the contents of the buffer.
ob_get_contents string ob_get_contents(void) returns the contents of the internal buffer. . This function returns the content of the current buffer, or FALSE if the output buffer is not activated.
ob_get_length int ob_get_length(void) This function returns the length of the current buffer; Like ob_get_contents, returns FALSE if the output buffer is not active.
ob_end_flush void ob_end_flush(void) Sends the contents of the internal buffer to the browser and closes the output buffer (if If so).
ob_end_clean void ob_end_clean(void) Delete the contents of the internal buffer and close the internal buffer. This function will not output the contents of the internal buffer!
ob_implicit_flush void ob_implicit_flush ([int flag]) Turn on or turn off absolute flushing Anyone who has used Perl knows the meaning of $|=x. This string can turn on/off the buffer, and the ob_implicit_flush function is the same as that. The default is to turn off the buffer and turn on absolute output
. 2. Example analysis:
1. Use buffer control functions to prevent errors in sending information in the file header.
//PHP prompt //PHP提示符
ob_start(); //Open buffer
echo "Welcome /n"; //Output
header("location:next.php"); / /Redirect the browser to next.php
?>
If ob_start is removed, PHP will prompt an error on line 4 of the file. The error message is "Header had all ready send by", but add ob_start , no error will be prompted. The reason is that when the buffer is opened, the characters after echo will not be output to the browser, but will remain in the server's buffer. They will not be output until you use flush or ob_end_flush, so it will not An error occurred that the file header has been output!
2. Save the output (this is a very classic use).
Suppose you want to know the screen output information of the client, such as the output result of the function, etc., and this output information will be different depending on the client. We can use the function phpinfo(); ?> to get the server's setting information, but what if we want to save the output of the phpinfo() function? Before there was no buffer control, it can be said that there was no way at all, but with buffer control, we can easily solve it.

ob_start(); //Open the buffer
phpinfo(); //Use the phpinfo function
$info=ob_get_contents(); //Get the contents of the buffer and assign it to $ info
$file=fopen('phpinfo.txt','w'); //Open the file phpinfo.txt
fwrite($file,$info); //Write information to phpinfo.txt
fclose($file); //Close the file phpinfo.txt
?>
Using the above method, you can save the phpinfo information of different users. This may not have been possible before! Similarly, the buffer method can be used to save tasks that are difficult to complete with ordinary methods. This is actually a method of converting some "processes" into "functions".

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446895.htmlTechArticlePHP4.0 provides a collection of output buffer functions. Output buffering support allows you to write functions that wrap functions around compressed buffers. Output buffering support in PHP4 allows HTML header information to be stored regardless of...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template