search
HomeBackend DevelopmentPHP Tutorialflush()函数有时好像不起作用

下面这个程序是我在网上找的,他注释中说第一句非常关键,可我发觉第一句根本没起作用,没有输出300个空格。而且我把第一句删了运行结果完全相同,这是为什么啊
for($i = 1; $i  // 这一句话非常关键,cache的结构使得它的内容只有达到一定的大小才能从浏览器里输出
// 换言之,如果cache的内容不达到一定的大小,它是不会在程序执行完毕前输出的。经
// 过测试,我发现这个大小的底限是256个字符长。这意味着cache以后接收的内容都会
// 源源不断的被发送出去。
For($j = 1; $j  echo $j." ";
flush(); //这一部会使cache新增的内容被挤出去,显示到浏览器上
sleep(1); //让程序"睡"一秒钟,会让你把效果看得更清楚
}
?>

我自己写了个程序,我发现把ob_flush()改成flush()后,第一条就不输出了,我记得flush()是立即输出缓冲区的内容啊,为什么不输出呢。
ob_end_flush();好像和ASP中的response.end也不同哦,ob_end_flush();后,后面的内容仍然会输出啊,有什么办法让后面的内容停止输出不


 
ob_start();
echo "第一条";
ob_flush() ; //立刻输出缓冲区中的内容
echo "第二条";
ob_get_clean() ; //清除缓冲区中的内容
echo "第三条" ;
ob_end_flush();
?>


回复讨论(解决方案)

只有 IE 才有缓冲区装满或传输结束才解释传入的内容的现象
空格不是 HTML 元素,无论多少个,也只会显示一个

flush() 的作用是将 php 系统缓冲区的内容推出到客户
ob_flush() 的作用是将 php 用户缓冲区的内容推出到 php 系统缓冲区
ob_end_flush() 的作用是将 php 用户缓冲区的内容推出到 php 系统缓冲区,并关闭 php 用户缓冲区
ob_end_clean() 的作用是将 php 用户缓冲区的内容清空,并关闭 php 用户缓冲区

response 采用的是完全不同的机制(response 是设备)
只有在 ob_start() 后的 ob_end_clean() 才相当于 response.end

我还是不太明白 系统缓冲区 和用户缓冲区,在网上也搜索不到这方面的资料,只是说PHP 支持多级缓冲区。好像是说可以建立n级缓冲区,我想是不是在缓冲区里又可以建缓冲区。这方面PHP比ASP复杂。

系统缓冲区 是指php的输出缓冲区,可用 output_buffering 控制其大小
每当 php 程序有输出时将存放在里边,待装满了或程序结束时一次性发送给用户
flush() 就是强制输出他里面的内容

用户缓冲区 是指用 ob_start() 开辟的缓冲区,每个 ob_start() 就是一个,允许嵌套

你试下这段代码,我在ie,firefox,chrome下都没问题。

<?phpecho str_pad('',4096);set_time_limit(50);for($i=0;$i<=5;$i++){    echo $i.'<br />';ob_flush();flush(); sleep(1);}?>

关于服务器端推送数据客户端即时显示的问题,其实是会受到web服务器,浏览器,以及代码三个方面的影响。

你要先明白整个流程:
web服务器也有缓冲区buffer,这个buffer的大小,会关系到服务器字节数到达多少后再推送给客户端浏览器。
然后浏览器也会有个buffer,收到多少字节后才显示出来。
你那段代码只是填充一些空白来欺骗浏览器让浏览器把内容即时显示出来。而问题可能在服务器端那边,根本没把数据发到客户端来。

对于服务器如何设置,apache貌似默认设置就可以。
nginx需要关闭gzip,然后设置fastcgi_buffers,fastcgi_buffer_size等值才行。具体你自己再查一下吧。

另外不同浏览器对于缓冲区buffer的规定也不一样,要想实现多种浏览器支持,你就尽量填大一些吧。

iis magager - system.webServer/handlers responseBufferLimit设为0, 
php.ini - output_buffer =Off

for($i = 1; $i 

飘过 只看版主的回答 打个记号

你试下这段代码,我在ie,firefox,chrome下都没问题。

<?phpecho str_pad('',4096);set_time_limit(50);for($i=0;$i<=5;$i++){    echo $i.'<br />';ob_flush();flush(); sleep(1);}?>
 感谢。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP vs. Python: Understanding the DifferencesPHP vs. Python: Understanding the DifferencesApr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP: Is It Dying or Simply Adapting?PHP: Is It Dying or Simply Adapting?Apr 11, 2025 am 12:13 AM

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

The Future of PHP: Adaptations and InnovationsThe Future of PHP: Adaptations and InnovationsApr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

When would you use a trait versus an abstract class or interface in PHP?When would you use a trait versus an abstract class or interface in PHP?Apr 10, 2025 am 09:39 AM

In PHP, trait is suitable for situations where method reuse is required but not suitable for inheritance. 1) Trait allows multiplexing methods in classes to avoid multiple inheritance complexity. 2) When using trait, you need to pay attention to method conflicts, which can be resolved through the alternative and as keywords. 3) Overuse of trait should be avoided and its single responsibility should be maintained to optimize performance and improve code maintainability.

What is a Dependency Injection Container (DIC) and why use one in PHP?What is a Dependency Injection Container (DIC) and why use one in PHP?Apr 10, 2025 am 09:38 AM

Dependency Injection Container (DIC) is a tool that manages and provides object dependencies for use in PHP projects. The main benefits of DIC include: 1. Decoupling, making components independent, and the code is easy to maintain and test; 2. Flexibility, easy to replace or modify dependencies; 3. Testability, convenient for injecting mock objects for unit testing.

Explain the SPL SplFixedArray and its performance characteristics compared to regular PHP arrays.Explain the SPL SplFixedArray and its performance characteristics compared to regular PHP arrays.Apr 10, 2025 am 09:37 AM

SplFixedArray is a fixed-size array in PHP, suitable for scenarios where high performance and low memory usage are required. 1) It needs to specify the size when creating to avoid the overhead caused by dynamic adjustment. 2) Based on C language array, directly operates memory and fast access speed. 3) Suitable for large-scale data processing and memory-sensitive environments, but it needs to be used with caution because its size is fixed.

How does PHP handle file uploads securely?How does PHP handle file uploads securely?Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

What is the Null Coalescing Operator (??) and Null Coalescing Assignment Operator (??=)?What is the Null Coalescing Operator (??) and Null Coalescing Assignment Operator (??=)?Apr 10, 2025 am 09:33 AM

In JavaScript, you can use NullCoalescingOperator(??) and NullCoalescingAssignmentOperator(??=). 1.??Returns the first non-null or non-undefined operand. 2.??= Assign the variable to the value of the right operand, but only if the variable is null or undefined. These operators simplify code logic, improve readability and performance.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.