PHP Cli Output after run - php

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.

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

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

Related

How to print current index or for loop in PHP?

The below PHP code returns 12345678910...... at a stretch.
for ($i=0; $i<1000; $i++) {
sleep(1);
echo $i;
}
How can I get it to print only the current loop number instead of printing all numbers together?
This can be easily done in php cli using the backspace character "\x08"
<?php
$length = 0;
for ($i=0; $i<1000; $i++)
{
// delete as much character as the length of the previous number
echo str_repeat("\x08", $length);
sleep(1);
echo $i;
// get the length of the number, so you know how much you have to delete
$length = strlen((string)$i);
}
This is my suggested answer (applicable to browser)
a) I believe the OP wants to have the output generated to the browser interface
b) To generate the output of the count during the 1-second sleep, we need to flush the output before each sleep
c) In a browser interface, it is not possible to generate a backspace, so let's do a javascript trick to update the div
<div id=output1></div>
<?php
$index=0;
while($index < 1000) {
ob_start();
echo "<script>document.getElementById('output1').innerHTML=" . $index . "</script>";
ob_end_flush();
#ob_flush();
flush();
sleep(1);
$index++;
}
?>

No output from the loop

I'm iterating over a huge array and it involves querying multiple APIs so to avoid data loss and timeout, I'm doing this:
for ($i = 0; $i < $count; $i++) {
if ($i % 100 == 0) {
echo 'processed: '.$i."\n";
// save to file
}
}
... it works if the loop is a few hundred iterations during test and outputs processed ..., but nothing at all in prod environment during the script is running, it echos everything only after its done. I just want to avoid any timeouts incase it takes (and usually does) long.
PHP output is buffered; see here. If you flush the buffer (or turn buffering off), you'll see it happen in real-time.
As "belt and braces", if you really care about the output, I'd echo the $count before you even enter the loop.
what is the aim of this code block?
well, until $i reaches 100, it is waste of resource anyway..
you can try:
if( $count > 100 ) {
ini_set('max_execution_time', 0); // no time limit
// $count contains at least $i times 100
for($i = 1; $i <= $count / 100; $i++) {
echo $i * 100;
}
}

php echo the result while iterating through the loop

I am execution loop and inside loop there is data processing function inside loop.
for($i = 0 ; $i <=680 ; $i = $i + 40)
{
$url = 'http://www.yelp.com/biz/franchino-san-francisco?start=80';
$root = yelp($url);
var_dump($root);
}
This loop takes long time to execute, and results are echoed at the end when entire loop completes.
How can I echo the result during each iteration?
Actually what happens here? does to result are stored in buffer and at the end echoed or what?
PHP buffers the output.
If you want to output stuff to the browser immediately you can use the flush() and ob_flush() functions:
for ($i = 0; $i <= 680; $i += 40) {
$url = 'http://www.yelp.com/biz/franchino-san-francisco?start=80';
$root = yelp($url);
var_dump($root);
flush();
ob_flush();
}
If you are executing PHP through a web-page, this would be the behaviour.
PHP is a server side language and all code will be executed before displaying the output to the client. (using a browser)
If you want to display the result within the loop, better use console / cmd (command line)
Here is something that will help you use PHP with commandline.

Display Real-Time Values

Say I have the following:
for ($i = 0; $i < 10; $i++) {
echo $i;
sleep(1);
}
Now, this will display:
0123456789
However, it will take 9 seconds to load, and will not display real-time.
How would I display it so it would be:
0(1 second)2(1 second)3(...)
My second question involves overwriting the current data on the page.
For example, say I have the same code as above. However, I want to display each number as itself. So the page would be:
0
Then after 1 second
1
And so on.
for ($i = 0; $i < 10; $i++) {
echo $i."\n";
$timeFirst = strtotime(date());
sleep(1);
$timeSecond = strtotime(date());
echo ($timeSecond - $timeFirst)." second(s) \n";
}
You can only do it with Client side script such as javascript. DOM loads only after the complete execution of your server side script.
There is no way you can execute each command and render the output to the web browser, unless you are doing it at CLI or doing it via Client Side.
Try this
for ($i = 0; $i < 10; $i++) {
echo $i;
flush();
ob_end_flush();
sleep(1);
}

php command line change text output

I want to know is it possible to change some output for special php cli base application to change some value on terminal not echo new one. for example this is cli application.
#!/usr/bin/env php
<?php
$percent = 0;
for ($i = 0; $i <= 100; $i++) {
echo $percent . "\n";
sleep(1);
$percent++;
}
/**
0
1
...
*/
It's a simple app to show the user the percentage. So we must update it after each loop in this example, rather than append it. I want to change percent not show new one.
use \r instead of \n. \r is a carriage return, it will jump back to the beginning of the line without a newline.
$percent = 0;
for ($i = 0; $i <= 100; $i++) {
echo $percent . "\r";
sleep(1);
$percent++;
}
This is working on Windows, Linux and MacOS
Something like this should work for what you need (tested and verified on a Linux (CentOS) machine):
for ($percent = 0; $percent <= 100; $percent++) {
echo $percent;
sleep(1);
// Print one or more backspaces, erasing current character(s)
echo str_repeat("\x08", strlen($percent));
}
This actually depends on your terminal (emulator) type, not on the language used. Have a few tries using the backspace character (0x08) to 'erase' the current content, then output the new content.

Categories