Bloggers are passionate about all kinds of internet technology, often wordy, often accompanied by obsessive-compulsive disorder, often updated, I think the article is helpful to you can pay attention to me. Reprint please indicate "The sickle of Deep Blue"
1. Problems encountered
I wonder if you've ever come across one of these two situations:
- The boss wants you to put all the data in the database in the browser display, because the need to compare data, the boss is not required to use paging , so good, output a 1000 of data directly loop print out no problem, but the data is millions of? Browser directly stuck, waiting for you to leave the seat out for a cup of coffee, a toilet, and the front desk mm chat for a day, come back to find the browser has "program not responding", what should you do?
- The boss lets you improve the website download link, requires the direct mouse left click on the download file (some file types, directly left click will open this file, many sites will prompt you "right-click Save As"), you use the header function and ReadFile function easy to achieve this function, but after the online found that if the file is too large browser will still be stuck, this time the boss will ask you to drink coffee, what should you do?
Well, if you run into either of these two situations, or you may face such problems in the future, you can mark it for the next quick fix.
2. Principle
.
The following is a grand introduction of the output buffer output control PHP
first, try the effect of the following code
<?phpif (ob_get_level () = = 0) {Ob_start ()}else{exit (0);};/ /start buffer for ($i = 0; $i <10; $i + +) { echo "line to show.\n<br/>";//not directly output, first in buffer Ob_flush ();// Output the buffer data flush ();//output the buffer data out of sleep (2);//pause for two seconds}echo "done"; o B_end_flush ();//close and clean buffer
Not Surprisingly, PHP can also implement this lazy loading function,is not very cow x feeling.
The principle is that PHP in the output data before the data into the
buffer , waiting for the need for us to output the buffered data, note that this is not to be confused with the
cache (caches) .
the benefits of doing so can be similar to lazy loading of the cool effect, on the one hand can also reduce the pressure on the server and the client, or output big data will appear when the memory is not enough.
Note:
Ob_flush () and
flush () are
used to brush out buffer data, but the official recommendation Since buffering can be done with only
Ob_flush () in most webserver, in some cases it is sometimes necessary to call
flush () in Apache, so for your code
portability, It is recommended to see
Ob_flush () immediately after adding
flush ().
Now that we know the principle, we can solve the two problems mentioned at the beginning.
3. Solve the problem of the million data single page output card die
<?phpob_start (); $data = [1,2,3,4,5,6,7,8,9,10];//actual data more, for convenient distance assume that the browser output 10 at a time will be stuck $per = 3;//Output 3 each time, can be changed to the $i = 0; $i < count ($data); $i + = $per) {for ($j = $i; $j < $i + $per && $j <count ($data); $j + +) { echo $data [$j]; } Ob_flush (); Flush (); Sleep (2);} echo "done."; O B_end_flush ();
4. Solve the problem that the file is too large to be jammed when the header implements the file download
<?phpheader (' content-type:application/txt ');//output Type Ob_start (); $data = "QWERTYUIOASDFGHJKL";//file contents, file_get_ Contents ($file) $per = 15;//15 characters per output, can be changed to 1000 or greater for ($i = 0; $i < strlen ($data); $i + = $per) {for ($j = $i; $j < $ i + $per && $j <strlen ($data); $j + +) { echo $data [$j]; } Sleep (2); Ob_flush (); Flush ();} echo "done.";
PHP outputs data row by line and resolves two common buffering problems