


ob cache introduction
ob is the abbreviation of output buffering, output buffer, and the buffer is controlled through the output_buffering variable in php.ini. Its default value is off and can be set to on to open the buffer. After calling the buffer, even if the ob function is not used in the program, the code actually uses the buffer. In addition, regardless of the setting of output_buffering in php.ini, php in cli mode is always turned off by default. Why a buffer? To put it simply, the high-speed CPU has processed its own data early and wants to transmit it to the user through the line, but the line is too narrow and cannot be transmitted at once. If a buffer is introduced, the CPU can quickly put the generated data into the buffer, and then rest somewhere cool. The buffer outputs data in a timely manner according to instructions. This effectively solves the contradiction between high-speed CPU and low-speed I/O devices.
Basic principles of ob: If the ob cache is turned on, the echo data is first placed in the ob cache. If it is header information, it is placed directly in the program cache. When the page is executed to the end, the ob cached data will be placed in the program cache, and then returned to the browser in turn.
The basic function of ob:
1) Prevent the use of setcookie(), header() or session_start() to send header files after the browser has output Error caused by function. In fact, it is better to use this kind of usage less often 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 be useful. .
3) Process the output content, such as gzip compression, conversion between Simplified and Traditional Chinese, and some string replacement.
4) Generating static files is actually capturing the output of the entire page and then saving it as a file. Often used in HTML generation or full page caching.
Detailed explanation of ob related functions
1. Flush: refresh the contents of the buffer and output.
Function format:
flush()
Description: This function is frequently used and is very efficient.
2. ob_start: Open the output buffer
Function format:
void ob_start(void)
Description: When the buffer is activated, all non-file header information from the PHP program will not be sent, but will be saved in Internal buffer. In order to output the contents of the buffer, you can use ob_end_flush() or flush() to output the contents of the buffer.
3, ob_get_contents: Return the contents of the internal buffer.
Usage:
string ob_get_contents(void)
Description: This function will return the contents of the current buffer. If the output buffer is not activated, it will return FALSE.
4. ob_get_length: Returns the length of the internal buffer.
Usage:
int ob_get_length(void)
Description: This function will return the length in the current buffer; the same as ob_get_contents, if the output buffer is not activated. then returns FALSE.
5. ob_end_flush: Send the contents of the internal buffer to the browser and close the output buffer.
Usage:
void ob_end_flush(void)
Description: This function sends the contents of the output buffer (if any).
6. ob_end_clean: Delete the contents of the internal buffer and close the internal buffer
Usage:
void ob_end_clean(void)
Description: This function will not output the contents of the internal buffer but delete it!
7. ob_implicit_flush: Turn on or turn off absolute flush
Usage:
void ob_implicit_flush ([int flag])
Description: Anyone who has used Perl knows the meaning of $|=x. This string can open/close the buffer. , and the ob_implicit_flush function is the same as that one. The default is to close the buffer. After turning on absolute output, each script output is sent directly to the browser, and there is no need to call flush()
The example code of the flush function is as follows:
<?php for($i = 1; $i <= 300; $i++ ) print(" "); // 这一句话非常关键,cache的结构使得它的内容只有达到一定的大小才能从浏览器里输出换言之,如果cache的内容不达到一定的大小,它是不会在程序执行完毕前输出的。经 // 过测试,发现这个大小的底限是256个字符长。这意味着cache以后接收的内容都会源源不断的被发送出去。 For($j = 1; $j <= 20; $j++) { echo $j.""; flush(); //这一部会使cache新增的内容被挤出去,显示到浏览器上 sleep(1); //让程序"睡"一秒钟,会让你把效果看得更清楚 } ?>
Description: flush() is a very efficient function. Its very useful function is to refresh the cache# of browser ##.
About the example code of the ob series function:
For example, you can use the setting information of the server and the client, but this information will be different depending on the client. , what if you want to save the output of the phpinfo() function? Before there was no buffer control, it can be said that there was no way at all, but with buffer control, we can easily solve it.<?php ob_start(); //打开缓冲区 phpinfo(); //使用phpinfo函数 $info=ob_get_contents(); //得到缓冲区的内容并且赋值给$info $file=fopen(\'info.txt\',\'w\'); //打开文件info.txt fwrite($file,$info); //写入信息到info.txt fclose($file); //关闭文件info.txt ?>Using the above method, you can save the phpinfo information of different users. This may not have been possible before! In fact, the above is a method to convert some "processes" into "functions"!
About the static template output example code:
<?php ob_start();//打开缓冲区 //php页面的全部输出 $content = ob_get_contents();//取得php页面输出的全部内容 $fp = fopen("output00001.html", "w"); //创建一个文件,并打开,准备写入 fwrite($fp, $content); //把php页面的内容全部写入output00001.html,然后…… fclose($fp); ?>The so-called static template technology is to use a certain method to make the user get the html page generated by PHP on the client side. . If this html page will no longer be updated, then when another user browses this page again, the program will no longer call PHP and related databases. For some websites with a large amount of information, technology like this will bring The benefits are huge. 【Related tutorial recommendations】1. "
php.cn Dugu Jiujian (4)-php video tutorial"
2. A complete set of tutorials on PHP programming from entry to master
The above is the detailed content of Introduction to php ob cache and detailed explanation of ob function. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools
