The output control function gives you the freedom to control the outputs of the data in your script. It is very useful, especially when you want to output a file header after the data has been output. The output control function does not affect the header information sent using header () or Setcookie (), only data blocks that resemble echo () and PHP (as the current mainstream development language) code.
Let's start with a simple example that gives you a general impression of the output control:
Example 1.
ob_start(); //打开缓冲区
echo "Hellon"; //输出
header("location:index.php(做为现在的主流开发语言)"); //把浏览器重定向到index.php(做为现在的主流开发语言)
ob_end_flush();//输出全部内容到浏览器
?>
All people who know about the header () function know that this function sends a file header to the browser, but if there are any outputs (including null output, such as spaces, carriage returns, and line feeds) before using this function, an error is indicated. If we get rid of the first line of Ob_start () and execute the program, we'll find an error message: "Header had all ready to send by"! But with Ob_start, there is no hint of error, because when the buffer is opened, the character behind the echo is not output to the browser, but remains on the server until you use flush or ob_end_flush to output, so there will be no file header output error!
Introduction of related functions
1, Flush: Flush the contents of the buffer, output.
function format: Flush ()
Description: This function is often used and is highly efficient.
2. Ob_start: Open Output buffer
function format: void Ob_start (void)
Note: When the buffer is active, all non-file header information from PHP (the current mainstream development language) program is not sent, but is saved in the internal buffer. In order to output the contents of the buffer, you can use the contents of the Ob_end_flush () or flush () output buffers.
3. Ob_get_contents: Returns the contents of the internal buffer.
How to use: String ob_get_contents (void)
Description: This function returns the contents of the current buffer and returns FALSE if the output buffer is not activated.
4. Ob_get_length: Returns the length of the internal buffer.
How to use: int ob_get_length (void)
Note: This function returns the length of the current buffer, as with ob_get_contents, if the output buffer is not activated. FALSE is returned.
5. Ob_end_flush: Sends the contents of the internal buffer to the browser, and closes the output buffer.
How to use: void Ob_end_flush (void)
Description: This function sends the contents of the output buffer (if any).
6. Ob_end_clean: Delete the contents of the internal buffer and close the internal buffer
How to use: void Ob_end_clean (void)
Description: This function does not output the contents of the internal buffer but deletes it!
7. Ob_implicit_flush: Turn absolute refresh on or off
How to use: void Ob_implicit_flush ([int flag])
Description: People who have used Perl know the meaning of $|=x, this string can open/close the buffer, and the Ob_implicit_flush function is the same, the default is to close the buffer, open the absolute output, each script output is sent directly to the browser, no longer need to call flush ()
Second, in-depth understanding
1. About the Flush function:
This function appears in PHP (now the mainstream development language) 3, is a very efficient function, he has a very useful function is to refresh the browser cache. Let's take a very clear example of how flush works.
Example 2.
for($i = 1; $i <= 300; $i++) print(" ");
// 这一句话非常关键,cache的结构使得它的内容只有达到一定的大小才能从浏览器里输出
// 换言之,如果cache的内容不达到一定的大小,它是不会在程序执行完毕前输出的。经
// 过测试,我发现这个大小的底限是256个字符长。这意味着cache以后接收的内容都会
// 源源不断的被发送出去。
for($j = 1; $j <= 20; $j++) {
echo $j . "
";
flush(); //这一部会使cache新增的内容被挤出去,显示到浏览器上
sleep(1); //让程序"睡"一秒钟,会让你把效果看得更清楚
}
?>
Specific effects you can go here to see http://www.php (as the current mainstream development language) 2000.com/~uchinaboy/out.php (as the current mainstream development language)
PHP (as the current mainstream development language) 2000 of the latest PHP (as the current mainstream development language) Chat room is the use of this technology, unfortunately the source code is not public L
Note: If you open an absolute refresh by adding Ob_implicit_flush () to the program's header, you can no longer use flush () in your program, and the benefit is: increase efficiency!
http://www.bkjia.com/PHPjc/508877.html www.bkjia.com true http://www.bkjia.com/PHPjc/508877.html techarticle the output control function gives you the freedom to control the outputs of the data in your script. It is very useful, especially when you want to output a file header after the data has been output. Output ...