Why does a PHP script wait "before the script completes" before any echo exit?

Given a simple script

<?php 
echo "hi";
foreach ($_GET['arr'] as $a)
{
 echo $a ."<br>";
}
echo "<p>Masel tov</p>";
foreach ($_GET['arr2'] as $a)
{
 echo $a ."<br>";
}

I expect the script to echo continuously. Instead, the script echoes when finished. Even the first " hi" gets an echo after 1 minute when the script ends.

Is there a way to prevent this, or why is this so?

+4
source share
5 answers

There is a function ob_implicit_flush that can be used to enable / disable automatic flushing after each output call. But check out the comments for the PHP manual before using it.

+2
source

, , ajax. , .

-, php ( ), .

+1
source

Check with this

if (ob_get_level() == 0)
{
    ob_start();
} 
for ($i = 1; $i<=10; $i++){
        echo "<br> Task {$i} processing ";
        ob_flush();
        flush();
        sleep(1);
}
echo "<br /> All tasks finished";
ob_end_flush();
0
source

PHP sends, as you noticed, all the data at once (at the moment the script ends) - you are looking for something like this http://de1.php.net/manual/de/function.ob-get-contents.php :

<?php

ob_start();

echo "Hello ";

$out1 = ob_get_contents();

echo "World";

$out2 = ob_get_contents();

ob_end_clean();

var_dump($out1, $out2);
?>
-1
source

All Articles