Reputation: 85
for($i = 0; $i < 5; $i++) {
echo $i . "\n";
sleep(1);
}
This is the code I run. Instead of showing a number each second, the php CLI decides to wait and after everything is executed shows
0
1
2
3
4
Why is this happening, how can I make it "real-time" ?
EDIT: Found the problem: because I included WP-Core (Wordpress) the output somehow buffered, if I remove the wp-core it is all fine. For more info, when including the wp_core there are some wp notices that are being logged in separate file.
Upvotes: 1
Views: 973
Reputation: 2617
You need to disable output buffering, or flush the buffer. You really only need the ob for requests that come over HTTP, as it lets you do all your server-side processing without having to worry about the connection to the client.
for($i = 0; $i < 5; $i++) {
echo $i . "\n";
sleep(1);
flush();
ob_flush();
}
Alternatively, if you can't change the code, you can disable output buffering completely in your CLI php.ini
output_buffering=Off
Upvotes: 2
Reputation: 7294
Servers
usually buffer the output of a server side script until there's enough in it to output try something like this. Combination of setting output buffering off and manually flushing the buffer. Note the implcit flush
line and the flush and ob_flush
lines.
for ($i = 0; $i < 5; $i++) {
echo $i . "</br>";
sleep(1);
flush();
ob_flush();
}
for more info about ob-flush
please read http://php.net/manual/en/function.ob-flush.php
Upvotes: 0