PHP显示的foreach输出到屏幕上,每个项目 [英] PHP showing output of foreach to screen, for each item

查看:778
本文介绍了PHP显示的foreach输出到屏幕上,每个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一件事情我已经注意到用PHP,是毫无输出到屏幕上,直到脚本已停止工作。对于这个项目我的工作我养活的超过100项的列表,并执行对每一个项目一个HTTP请求和完成时,显示一个页面,每个项目的状态,成功失败等。

One thing I have noticed with php, is that nothing is output to the screen until the script has stopped working. For the project I am working on I feed in a list of over 100 items and it performs a HTTP request for each item and when finished, shows a page with the status of each item, success failure etc.

如果有一种方法可以输出每一个的foreach循环的结果,因为他们发生什么,我想知道的是?所以,看着屏幕上的用户所看到的神奇发生的一行时间或说5行之后。

What I want to know is if there is a way to output the results of each 'foreach' loop as they happen? So the user watching the screen sees the magic happening one line at a time or after say 5 lines.

我只看过这个工作使用Ajax类型的请求,是我应该寻找做,而不是可能?任何人都可以点我一个PHP函数,它这个还是不可能?

I have only ever seen this done with Ajax type requests, is that what I should be looking to do instead maybe? Can anyone point me to a php function that does this or is it not possible?

推荐答案

这可能是更好的存储在缓冲区中的所有脚本输出然后在需要时刷新缓冲区。

It may be better to store all script output in a buffer then flush the buffer when required.

例如:

<?php

if (ob_get_level() == 0) ob_start();

$test = Array('one','two','three','four');
foreach ($test as $key=>$val)
{
    echo $test;
    ob_flush();
    flush();
}

ob_end_flush();

?>

请确保你已经MOD_GZIP禁用!

Make sure you have mod_gzip disabled!

这篇关于PHP显示的foreach输出到屏幕上,每个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆