flush()函数有时好像不起作用
下面这个程序是我在网上找的,他注释中说第一句非常关键,可我发觉第一句根本没起作用,没有输出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 <= 300; $i++ ) print(" ");
飘过 只看版主的回答 打个记号
你试下这段代码,我在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);}?>

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

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.
