: This article mainly introduces PHP's row-by-row data output and solves two common buffer problems. For more information about PHP tutorials, see. Bloggers are keen on a variety of Internet technologies. they are often nagging and often accompanied by obsessive-compulsive disorder and are often updated. if they think that the articles are helpful to you, they can pay attention to me. For more information, see "Deep Blue sickle"
1. problems encountered
I wonder whether you have encountered the following two situations:
- The boss wants you to display all the data in the database in a browser. because you need to compare the data, the boss requires that you cannot use paging, it's okay to print out 1000 pieces of data in a loop. but how many million pieces of data? The browser is stuck. when you leave your seat and have a cup of coffee, go to the toilet, and chat with the front-end mm for a day, you will find that the browser has "no response to the program, what should you do?
- The boss asked you to improve the download link of the website and asked you to press the left button to download the file. (for some file types, if you press left, the file will be opened, many websites will prompt you to "right-click and save ").HeaderFunctions andReadfileThe function easily implements this function, but after going online, it finds that if the file is too large, the browser will still be stuck. what should you do if the boss wants to invite you to drink coffee this time?
Well, if you encounter the above two situations or you may face these problems in the future, you can mark them so that they can be solved quickly next time.
2. Principles
Let's get down to the truth.
The following introduces the output buffer of PHP output control.
First, try the following code.
"; // No direct output, first saved to the buffer zone ob_flush (); // output the data in the buffer zone to flush (); // output the data in the buffer zone to sleep (2); // pause for two seconds} echo "Done. "; ob_end_flush (); // close and clear the buffer
I can't think of PHP's ability to implement this delayed loading function. isn't it really cool.
The principle is that PHP puts the dataBuffer)Wait for the buffer data to be output.Cache)Obfuscation.
The benefits of doing so can achieve cool effects similar to delayed loading, and reduce the pressure on servers and clients. otherwise, insufficient memory will occur when big data is output.
Note:Ob_flush ()AndFlush ()Buffer data is flushed out in usage, but we recommend that you use it in combination because most webservers only useOb_flush ()But in some cases, for example, apache sometimes needs to callFlush ()So for your codePortability,We recommend that you seeOb_flush ()AddFlush ().
Now that we know the principle, let's solve the two problems mentioned at the beginning.
3. solve the problem of millions of data Single Page output freezing
4. solve the problem that the file is too large during header implementation and becomes stuck
The above introduces PHP's line-by-line data output and solves two common buffering problems, including some content. I hope that my friends who are interested in PHP tutorials can help me.