


1. buffer ---- flush()
buffer is a memory address space. The default size of the Linux system is generally 4096 (1kb), which is one memory page. It is mainly used to store data transfer areas between devices with unsynchronized speeds or devices with different priorities. Through the buffer, the processes can wait less for each other. Here is a more general example. When you open a text editor to edit a file, every time you enter a character, the operating system will not immediately write the character directly to the disk, but first write it to the buffer. When writing When a buffer is full, the data in the buffer will be 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.
Similarly, when echo and print are executed, the output is not immediately transmitted to the client browser for display through tcp, but the data is written to the php buffer. The php output_buffering mechanism means that a new queue is established before the tcp buffer, and data must pass through the queue. When a php buffer is full, the script process will hand over the output data in the php buffer to the system kernel and pass it to the browser via TCP for display. Therefore, the data will be written to these places in sequence echo/pring -> php buffer -> tcp buffer -> browser
2. php output_buffering --- ob_flush()
By default, php buffer is turned on, and the default value of the buffer is 4096, which is 1kb. You can find the output_buffering configuration in the php.ini configuration file. When echo, print, etc. output user data, the output data will be written to php output_buffering. Until output_buffering is full, the data will be sent to the browser through tcp. show. You can also manually activate the php output_buffering mechanism through ob_start(), so that even if the output exceeds 1kb of data, the data is not actually handed over to tcp and passed to the browser, because ob_start() sets the php buffer space to a large enough size . The data will not be sent to the client browser until the end of the script or the ob_end_flush function is called.
The use of these two functions is probably the most confusing issue for many people. The explanation of the two functions in the manual is not clear, and their differences are not clearly pointed out. It seems that the functions of both are the same. is to flush the output cache. But in the code at the beginning of our article, if flush() is replaced with ob_flush(), the program will no longer execute correctly. Obviously, there is a difference between them. Otherwise, it would be enough to directly state in the manual that one of them is an alias of another function. There is no need to explain them separately. So what is the difference between them?
When caching is not enabled, the content output by the script is in a state of waiting for output on the server side. flush() can immediately send the content waiting for output to the client.
After the cache is turned on, The content output by the script is stored in the output cache. At this time, there is no content waiting for output. If you use flush() directly, it will not send any message to the client. content. The function of ob_flush() is to take out the content that originally existed in the output cache, and set it to the waiting output state, but it will not be sent directly to the client . In this case, you need to use ob_flush() first and then Using flush(), the client can immediately get the output of the script.
3. The correct order of flush and ob_flush is, ob_flush first and then flush, as follows:
ob_flush();
flush();
If the Web server If the operating system is Windows, then there will be no problem if the order is reversed or if ob_flush() is not used. [To be verified ] However, the output buffer cannot be refreshed on Linux systems.
4. output buffering function
bool ob_start ([ callback $output_callback [, int $chunk_size [, bool $erase ]]] )
Activate the output_buffering mechanism. Once activated, the script output is no longer sent directly to the browser, but is temporarily written to the PHP buffer memory area.
php enables the output_buffering mechanism by default, but the data output_buffering value is expanded to a large enough value by calling the ob_start() function. You can also specify $chunk_size to specify the value of output_buffering. The default value of $chunk_size is 0, which means that the data in the php buffer will not be sent to the browser until the end of the script. If you set the size of $chunk_size, it means that as long as the data length in the buffer reaches this value, the data in the buffer will be sent to the browser.
Of course, you can process the data in the buffer by specifying $ouput_callback. For example, function ob_gzhandler compresses the data in the buffer and then sends it to the browser.
The third parameter: whether to erase the cache, optional, the default is true, if set to false, the cache will not be cleared before the script execution ends.
ob_get_contents
Get a copy of the data in the php buffer. It is worth noting that you should call this function before the ob_end_clean() function is called, otherwise ob_get_contents() returns a null character.
You can use ob_get_contents() to obtain the data cached by the server in the form of a string.
Using ob_end_flush() will output the cached data and close the cache.
Using ob_end_clean() will silently clear the data cached on the server without any data or other actions.
The caches on the server are stacked, which means that after you enable ob_start() and before closing it, you can open another cache ob_start() inside it.
But you must also ensure that the number of operations to turn off the cache is the same as the number of operations to turn on the cache.
ob_start() can specify a callback function to process cache data . If one ob_start() is nested inside another ob_start(), we assume that the outer The ob_start() number of the layer is A, and the ob_start() number of the inner layer is B. They each have a callback function called functionA and functionB. Then when the data in cache B is output, it will be processed by the funcitonB callback function. , and then handed over to the outer functionA callback function for processing, and then can be output to the client.
In addition, the manual says that for some web servers, such as apache, using the callback function may change the current working directory of the program. The solution is to manually modify the working directory back in the callback function, using The chdir function does not seem to be encountered often. Remember to check the manual when you encounter it.
ob_end_flush and ob_end_clean
These two functions are somewhat similar and both turn off the ouptu_buffering mechanism. But the difference is that ob_end_flush only flushes (flush/send) the data in the php buffer to the client browser, while ob_clean_clean clears (erase) the data in the php bufeer but does not send it to the client browser.
Before ob_end_flush is called, the data in the php buffer still exists, and ob_get_contents() can still obtain a copy of the data in the php buffer.
After calling ob_end_flush(), ob_get_contents() gets an empty string, and the browser cannot receive output, that is, there is no output.
You can use ob_get_contents() to obtain the server-side cached data in string form, and use ob_end_flush() to output the cached data and close the cache.
Using ob_end_clean() will silently clear the data cached on the server without any data or other actions.
The caches on the server are stacked, which means that after you enable ob_start() and before closing it, you can open another cache ob_start() inside it. However, you must also ensure that there are as many operations to turn off the cache as there are operations to turn the cache on.
ob_start() can specify a callback function to process the cached data. If one ob_start() is nested inside another ob_start(), we assume that the outer ob_start() number is A, and the inner ob_start() ) number is B. They each have a callback function, functionA and functionB. Then when the data in cache B is output, it will be processed by the funcitonB callback function first, and then handed over to the outer functionA callback function for processing before it can be output. to the client.
In addition, the manual says that for some web servers, such as apache, using the callback function may change the current working directory of the program. The solution is to manually modify the working directory back in the callback function, using The chdir function does not seem to be encountered often. Remember to check the manual when you encounter it.
The above is the detailed content of Detailed explanation of the usage difference between flush() and ob_flush() functions in php. 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字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

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

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

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

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

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.