ob_start()
I only talk about its functions, not how to use it. You can find it yourself. It is all available online. Today I just want to help people who are a little confused clarify it.
Yesterday I saw a code from a friend’s company that is similar to the code above. To put it bluntly, it is meaningless code (I asked, not for the purpose of setcookie header).
After I thought about it carefully, I searched online and found that quite a few beginners (technical beginners, not necessarily first-year PHP students, some people have been beginners their entire lives) do not understand the role of ob. , but ob is often called output buffer and output cache on the Internet, so many people regard the ob series functions as tools to speed up PHP page display.
In fact, ob is the abbreviation of output buffering, not output cache. If ob is used correctly, it can help the speed to a certain extent, but blindly adding the ob function will only increase the extra burden on the CPU. Next I will talk about the basic function of ob.
1. Prevent errors caused by using setcookie, header, or session_start functions after the browser has output. (I originally thought that the code mentioned at the beginning had such a function, but later my friend said it was not the case). In fact, it is better to use this kind of usage less and develop good coding habits.
2. Capture the output of some unobtainable functions. For example, phpinfo will output a lot of HTML, but we cannot use a variable such as $info=phpinfo(); to capture it. At this time, ob will come in handy
3. Process the output content, such as gzip compression, conversion between Simplified and Traditional Chinese, and some string replacement.
4. Generate static files, which is actually to capture the output of the entire page and then save it as a file. It is often used in HTML generation or full page caching.
Regarding the GZIP compression mentioned in the third point just mentioned, many people may want to use it, but have not really used it. In fact, by slightly modifying my friend's code, you can achieve gzip compression of the page.
ob_start();
....
ob_clean();
Any output between these two sentences can be obtained using ob_get_contents(); without outputting it to the browser.
Classic application: Write phpinfo() information to a file.
ob_start();
phpinfo();
$phpinfo = ob_get_contents();
//File read and write operations
ob_clean();
http://www.bkjia.com/PHPjc/477867.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477867.htmlTechArticleob_start() only talks about its function, not how to use it. You can find it yourself how to use it. It’s all available online. Today I’m just here to help. People who were a little confused became clear. Yesterday I saw the code of a friend’s company that was similar to...