找到你要的答案

Q:Display live debug messages while running a function in PHP with Ajax

Q:展示现场调试消息而运行PHP AJAX功能

I have a function like this that may run for 20 minutes:

function  test($videoarray)
{
    for($i;$i<100;$i++)
        if(converVideo($videoarray[$i]))
        {
           echo 'video number '.$i.' has been convert';
        }
        else
        {
           echo 'video number '.$i.' Is too large.';
        } 
}

During this time I wish I could see the messages live, for example with Ajax/PHP.

Is it possible?

Desired result:

1st min:  video number 1 has been converted.
2nd min:  video number 2 is too large.
,
.
.

我有这样的功能,可以运行20分钟:

function  test($videoarray)
{
    for($i;$i<100;$i++)
        if(converVideo($videoarray[$i]))
        {
           echo 'video number '.$i.' has been convert';
        }
        else
        {
           echo 'video number '.$i.' Is too large.';
        } 
}

在这段时间,我希望我能看到的信息生活,例如Ajax与PHP。

这可能吗?

预期的结果:

1st min:  video number 1 has been converted.
2nd min:  video number 2 is too large.
,
.
.
answer1: 回答1:

I would recommend adding flush(); after each echo

This should force the message to the screen once written.

Example

echo 'video number '.$i.' has been convert';
flush();

PHP FLUSH

This might not always work therefore one can always persist the messages to a database and have a polling mechanism to read the message every couple of seconds.

EXAMPLE of PHP Polling

我会建议增加flush();每次的回声

这应该迫使消息一旦写入屏幕。

例子

echo 'video number '.$i.' has been convert';
flush();

PHP冲洗

这可能并不总是工作,因此,可以始终坚持到数据库的消息,并有一个轮询机制每隔几秒读取消息。

PHP投票的例子

php  ajax  debugging