调用ob_flush()和flush(),但是浏览器在脚本完成之前不会显示任何输出 [英] Calling ob_flush() and flush(), yet browser doesn't show any output until script finishes

查看:93
本文介绍了调用ob_flush()和flush(),但是浏览器在脚本完成之前不会显示任何输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 请查看以下代码:

<?php
ob_start();

echo "Start ...<br />\n";
for( $i = 0 ; $i < 10 ; $i++ )
{
    echo "$i<br />\n";
    ob_flush(); 
    flush();
    sleep(1);
}
echo "End ...<br />\n";
?>

这是不正确的?我已经对其进行了测试,但是我的输出显示了脚本执行完毕后的解决方案吗?

It's Incorrect ? i'm tested it but my output show when script is done, have any solution ?

推荐答案

嘿,我也陷入了这个问题 终于得到了正确的解决方案 这是给你的

Hey man I was also got stuck in this problem and finally got the correct solution here it is for you

您必须为页面添加内容类型 你可以通过两种方式做到这一点 1.使用html标记

you have to add content type for your page you can do that by two ways 1. using html tag

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

例如.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Wp Migration</title>
</head>
<body>
<?php 
for($i=0;$i<70;$i++)
{
echo 'printing...<br>';
ob_flush();
flush();
sleep(3);
}
?>
</body>
</html>

  1. 使用php标头功能

  1. using php header function

<?php header( 'Content-type: text/html; charset=utf-8' ); ?>

例如.

<?php 
header( 'Content-type: text/html; charset=utf-8' );
for($i=0;$i<70;$i++)
{
echo 'printing...<br>';
ob_flush();
flush();
sleep(3);
}
?>

祝一切顺利

这篇关于调用ob_flush()和flush(),但是浏览器在脚本完成之前不会显示任何输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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