The use of Ob_* series functions in PHP

Source: Internet
Author: User

Using PHP to control the user's browser--ob_* series functions

The output control function gives you a free rein in the outputs of the data in the script. It's very useful, especially for: when you want to output the file header after the data has been exported. Output control functions do not use header () or Setcookie (), send header information to affect, only for those similar to echo () and PHP code blocks of data have effect. Let's start with a simple example that gives you a general impression of 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 you have any output (including null output, such as a space, a carriage return, or a newline) before using this function, you will be prompted for an error. If we remove the first line of Ob_start () and then execute the program, we will find an error message: "Header had all ready send by"! But with Ob_start, you won't be prompted for an error, because when the buffer is open, the characters behind Echo are not exported to the browser, but remain on the server until you use flush or ob_end_flush to output, so there is no error in the header output!

A brief introduction of related functions

1, Flush: Refresh the contents of the buffer, output. function format: Flush () Description: This function is often used, high efficiency.

2. Ob_start: Open Output buffer function format: void Ob_start (void) Description: When the buffer is activated, all non-file header information from the PHP program is not sent, but is saved in the internal buffer. To output the contents of the buffer, you can use Ob_end_flush () or flush () to output the contents of the buffer.

3, Ob_get_contents: Return the contents of the internal buffer. Use method: String ob_get_contents (void) Description: This function returns the contents of the current buffer, FALSE if the output buffer is not active.

4, Ob_get_length: Returns the length of the internal buffer. Usage: int ob_get_length (void) Description: This function returns the length of the current buffer, as in ob_get_contents, if the output buffer is not active. Returns FALSE.

5, Ob_end_flush: Send the contents of the internal buffer to the browser, and turn off the output buffer. Usage: 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 use method: void Ob_end_clean (void) Description: This function will not output the contents of the internal buffer but delete it!

7. Ob_implicit_flush: Turn on or off absolute refresh usage: 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 ob_ Implicit_flush function is the same as that, the default is to close the buffer, open absolute output, each script output is sent directly to the browser, no longer need to call flush ()

Second, in-depth understanding of 1. About the Flush function: This function appears in PHP3, is a very efficient function, he has a very useful function is to refresh the browser cache. We give a very obvious example of the effect of the operation of Flush.

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); //让程序"睡"一秒钟,会让你把效果看得更清楚
}

The specific effect you can come here to see http://www.php2000.com/~uchinaboy/out.php PHP2000 's latest PHP chat room is the use of this technology, but unfortunately the source code is not open L

Note: If the program's first join Ob_implicit_flush () to open absolute Refresh, you can no longer use flush () in the program, the benefit is: improve efficiency!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.