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);
}
Related
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++;
}
?>
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;
}
}
i have this code
$number = 1;
echo $number;
for ($i=0; $i < 10; $i++) {
$number++;
}
the output of echo $number is 1 not 11.
How can get the last $number value when I called it before it changed?
Once you output something to the browser, it's done. You cannot change it again later. The only way to handle this is to not output the variable until you have found its final value; ie in the example you move the echo statement to the bottom.
It's generally considered a good idea to first run all of your PHP code and determine all your variables and only then start outputting things to the browser in order to prevent the kind of problem you have now.
$number = 1;
echo 'before increment :'.$number;
for ($i=0; $i < 10; $i++) {
$number++;
}
echo 'after increment :'.$number;
Try this way, now you will get expected result:
$number = 1;
for ($i=0; $i < 10; $i++) {
$number++;
}
echo $number;
Reason, you have put echo $number before the increment, which was logically wrong:
I am working on a project for a friend's website which is suppose to generate completely random phone numbers to be displayed on a "fake" review board. I figured the best way to do with would be for me to to generate out each section separably. So 3-3-4, but no matter what I do, every time there is a 0 in front the code cuts it off. Here's an example of what I mean:
http://www.shiningashes.net/Test.php
yet this is what I have for the code:
<?php
for ($i = 0000; $i <= 9999; $i++) {
echo $i;
echo "<br>";
}
?>
How do I get the 0's to stop being cropped out so the 0's display? 0001, 0021, 0123, etc?
You can use str_pad
for ($i = 0; $i <= 9999; $i++) {
echo str_pad($i, 4, '0', STR_PAD_LEFT);
echo "<br>";
}
You can use printf to format your output:
<?php
for ($i = 0; $i <= 9999; $i++) {
printf("%04d<br>\n",$i);
}
?>
You need to make your variable a string if you want to keep the zeros. That would mean using quotes, and never using numeric operators on it. But since you depend on using ++ on it, I suggest the following hack:
<?php
for ($i = 10000; $i <= 19999; $i++) {
$str=substr ( $i , 0 , 4 );
echo $i;
echo $str;
echo "<br>";
}
?>
You will need to convert your integer to a string when printing it.
<?php
for ($i = 0; $i <= 9999; $i++) {
printf("%04d<br />", $i);
}
?>
Check the documentation for printf/sprintf for more information.
Kind regards,
Stefan
Alright, I'm working on a (supposed-to-be) simple counting script using ncurses. Everytime it increments the number, I need it to delete the previous number before adding another number, so that it updates rather than appends.
Here is my code:
<?php
ncurses_init();
$i = 0;
$nStr = "Number: ";
ncurses_addstr($nStr);
ncurses_refresh();
for ($i=0; $i < 100; $i++)
{
$iLen = strlen($i);
for ($j=0; $j < $iLen; $j++)
{
ncurses_delch();
}
ncurses_addstr($i);
ncurses_refresh();
sleep(2);
}
ncurses_end();
?>
Currently when I run it, it outputs like this: Number: 01234[...]
Anyone see where my problem is and how I can fix it?
ncurses_delch() forward-deletes. If you want to move the cusor back one column then output \b instead.