Home > Backend Development > PHP Tutorial > PHP Output Control (Output Control) Learning_PHP Tutorial

PHP Output Control (Output Control) Learning_PHP Tutorial

WBOY
Release: 2016-07-13 10:11:30
Original
933 people have browsed it

PHP Output Control (Output Control) Learning

Introduction to php buffering
Actually, my impression of the php ob series is still very vague. I don’t know much about how to play it. I usually use curd and I really don’t have an in-depth understanding of these contents. As a phper, I am very ashamed. net
After searching online and copying each other, the code does not show the phenomenon described by the author. This article is a conscientious product, and the code has been run by the author.
When executing output, such as echo, print. The output is not sent to the web server immediately, but the data is written to the php buffer. php output_buffering mechanism
The benefit is of course improved performance. In fact, the php file is finally displayed on the browser and goes through three buffering stages: php buffer=》web server buffer=》browser buffer. Last shown
Show to browser
By default, php buffer is turned on, and the default value of the buffer is 4096, which is 4 kb. You can find the output_buffering configuration in the php.ini configuration file
Set. Buffer is a memory address space. The default size of Linux system is generally 4096 (4kb), which is one memory page. Mainly used for devices with unsynchronized storage speeds or devices with different priorities
The area for transferring processing data between
. Through the buffer, the processes can wait less for each other. Here is a simple example. When you open a text editor to edit a file
, every time you enter a character, the operating system will not write the character directly to the disk immediately, but first write it to the buffer. When the buffer is full, it will write the character into the buffer
The data of
is written to the disk. Of course, when the kernel function flush() is called, it is mandatory to write the dirty data in the buffer back to the disk.
Give an example
echo "Namo Amitabha
";
header("content-type:text/html;charset='utf-8'");
echo "Truthfulness-Compassion-Forbearance is good!";
//output
//Namo Amitabha
//Truthfulness-Compassion-Forbearance is good
header() must be called before any actual output, but our program has already output, but it runs normally. Looking at the code below:
echo "Namo Amitabha
";
ob_flush();
header("content-type:text/html;charset='utf-8'");
echo "Truthfulness-Compassion-Forbearance is good!";
//output
//Namo Amitabha
//Cannot modify header information - headers already sent by (output started at E:phptest.php:3)
//Truthfulness-Compassion-Forbearance is good
The above program shows that the program does not output immediately, but only refreshes the buffer and outputs when the ob_flush function is called.
ob_flush() and flush()
ob_flush(), flush() functions have detailed instructions in the PHP manual, you can check them out. The difference between the two is:
ob_flush() is to refresh PHP’s own buffer
flush() Yes It refreshes the WebServer server's buffer. Output to browser. But the following situation will occur:
Some web server programs, especially web server programs under Win32, will still cache the output of the script until the end of the program before sending the results to the browser.
Some Apache modules, such as mod_gzip, may perform output caching by 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 before it receives a newline or the beginning of an html tag, and it caches content before it receives a
The entire table will not be displayed before the
tag.
Some versions of Microsoft Internet Explorer will only start displaying the page after receiving 256 bytes, so some extra spaces must be sent to allow these browsers
Show page content.
For example:
/**
Server:LightTPD/1.4.28 (Win32)
X-Powered-By:PHP/5.3.27
*/
echo 'The Dharma is boundless'."
";
ob_flush();
flush();
sleep(1);
echo 'The wheel of law always turns';
//output
The above code outputs line by line on the Chrome browser, but outputs all on the IE series browsers. In fact, it is the fourth one above. Some browsers can only receive 256
characters will be displayed. Change the above code to the following form:
/**
Server:LightTPD/1.4.28 (Win32)
X-Powered-By:PHP/5.3.27
*/
echo str_pad('',240)."n";
echo 'The Dharma is boundless'."
";
ob_flush();
flush();
sleep(1);
echo 'The wheel of law always turns';
//output
In this way, it will be output line by line under IE because it exceeds 256 characters.
ob Other function descriptions
1.ob_end_flush and ob_end_clean
End just as the name suggests, close the buffer, both close the output buffer, one is the output buffer, the other is clearing. For example
/**
Server:LightTPD/1.4.28 (Win32)
X-Powered-By:PHP/5.3.27
*/
echo 'before';
ob_end_clean();
echo str_pad('',4096)."n";
for ($i=10; $i>0; $i--)
{
echo $i;
sleep(1);
}
The above code outputs the entire content at once, rather than outputting it one by one. Doesn't ob_end_clean() turn off the buffer? Why not output them one by one? In fact, we also said it above,
php is not output directly to the browser, but to the web server. Although php has no buffering. But the web server still exists. So the following code:
/**
Server:LightTPD/1.4.28 (Win32)
X-Powered-By:PHP/5.3.27
*/
echo 'before';
ob_end_clean();
echo str_pad('',4096)."n";
for ($i=10; $i>0; $i--)
{
flush();
echo $i;
sleep(1);
}
Add flush() and it will be output line by line. If ob_end_clean is replaced by ob_end_flush, before will be output.
For other functions, please refer to the manual, which is relatively simple.
Summary
From the php script to the browser, you need to go through php buffer=》web server buffer=》browser buffer. Finally displayed to the browser. Both are indispensable. So we have to
ob_flush and flush and echo str_pad('',4096) can be used to debug the effect you want.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/928279.htmlTechArticlePHP Output Control (Output Control) Learning PHP Buffer Introduction In fact, my impression of the PHP ob series is still very vague, specific I don’t know much about how to play. I usually curd, but I am really familiar with these...
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