Home > Backend Development > PHP Tutorial > PHP outputs data and organizes it while executing

PHP outputs data and organizes it while executing

WBOY
Release: 2016-07-29 08:56:01
Original
1196 people have browsed it
//ignore_user_abort(true);//是否忽略浏览器的断开而继续执行脚本
header( 'Content-Type: text/html;charset=utf-8' );
echo str_pad("",1000);   //输出1000个空格(浏览器需要接受一定长度的数据之后才会输出内容)
echo 'begin...<br>';
ob_flush();
flush();
for($i=0;$i<10;$i++)
{
    echo 'loading '.$i.'0%<br>';
	
    if ( connection_aborted() )//检查是否断开客户机。 如果已终止连接,则该函返回 1,否则返回 0
    {
        exit;
    }
	
	ob_flush();
    flush();
   
    sleep(1);//睡眠一秒
}
 
echo 'ok';
Copy after login
/*
First clarify the output order of PHP
1. Turn on the php output cache: echo,print -> php output_buffring -> server buffering -> browser buffering -> browser display
2. Not open php Output cache: echo, print -> server buffering -> browser buffering -> browser display
Also clarify the browser's output cache: IE is 256Bytes, Chrome and FireFox are 1000Bytes, only the output data reaches this length or script The data will be output on the page only after the browser is terminated
Let’s talk about several PHP settings and APIs used:
output_buffering configuration in 1.php.ini
?Off: means turning off PHP output caching
?On: turning on unlimited Large output cache
?4096: Turn on the output cache with a size of 4096Byte
Implicit_flush configuration in 2.php.ini
?On: Indicates that after each output (such as echo, print), the flush() function is automatically called and the output is directed
?Off: Contrary to On, flush() will not be called after each output. It needs to wait until the server buffering is full before outputting. However, we can use the flush() function to replace it. It doesn’t matter if it is not enabled, but it is more flexible
3 .ob_flush() function: Take out the data from PHP buffering and put it into server buffering
4.flush() function: Take out the data from Server buffering and put it into browser buffering
5.ob_start() function: Open a buffer on the server Save all output. So anytime echo is used, the output will be added to the buffer until the program ends or is terminated using ob_flush(). Then the contents of the buffer in the server will be sent to the browser, which will be parsed and displayed by the browser.
ob_* series of functions operate the output buffer of PHP itself.
ob_get_contents() - Returns the contents of the output buffer
ob_flush( ) - flush out (send out) the contents of the output buffer
ob_clean() - clear (erase) the output buffer
ob_end_flush() - flush out (send out) the contents of the output buffer and close the buffer
ob_end_clean() - clear ( Erase) buffer and close the output buffer
flush() - Flush the output buffer
Summary:
ob_flush is to flush PHP's own buffer.
The function ob_end_clean will clear the contents of the buffer and close the buffer, but will not output content.
At this time, a function ob_get_contents() must be used in front of ob_end_clean() to obtain the contents of the buffer.
In this case, the content can be saved to a variable before executing ob_end_clean(), and then the variable can be operated after ob_end_clean()
Can be used to cache static html content
Note: flush, strictly speaking, This only has practical effect when PHP is installed as a Module (handler or filter) of apache. It refreshes the buffer of WebServer (which can be considered to refer specifically to apache).
1. Under the sapi of apache module, flush will By calling the flush member function pointer of sapi_module, the api of apache is indirectly called: ap_rflush refreshes the output buffer of apache. Of course, the manual also says that there are some other modules of apache that may change the result of this action..
2 .Some Apache modules, such as mod_gzip, may perform output caching themselves, which will cause the results generated by the flush() function to not be sent to the client browser immediately.
Even the browser will cache the received content before displaying it. For example, the Netscape browser caches content until it receives a newline or the beginning of an html tag, and does not display an entire table until it receives a tag.
3. Some versions of Microsoft Internet Explorer will only start to display the page after receiving 256 bytes, so some additional spaces must be sent to allow these browsers to display the page content.
So, the correct order to use the two is: ob_flush first, then flush.
Of course, under other sapi, you can not call flush, but in order to ensure the portability of your code, it is recommended to use it together.

*/

Source:

http:// bbs.csdn.net/topics/310167610

http://my.oschina.net/miaowang/blog/349290

http://www.cnblogs.com/daxian2012/archive/2012/ 09/12/2682136.html

Thank you all for your selfless sharing!

The above introduces the sorting of data output while executing PHP, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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