-1

I’m trying to see the evolution of the variable X, so that then I will be able to see the increment of the variable with slow speed.

When I try this code in a local host using the software XAMPP, the PHP code is working correctly, so that I’m able to see a new value of X every new second.

When I try the same code in a web hosting (FreeWHA) The webpage completely freezes until the while loop “is going”, And once the cycle is ended the page will load all the output printed all at once.

this below is the very basic code:

<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>

</head>
<body>

    <?php
    $x=0;
        while($x<100) {  //print first 100 values
            $x = $x + 1;
            echo "x = ". $x. "</br>";
            ob_flush();
            flush();
            usleep(1000000);     //1 seconds = 1000000
           }
    ?>
    
</body>
</html>

What should I do to make the web hosting page that I’m using to print the output of the variable X gradually?

How can I perform this task?

Riemann
  • 67
  • 9
  • 2
    ditch this idea and use clientside code like a basic setInterval, or if you actually want something from server use, websocket, server-sent events or ajax – Lawrence Cherone Jan 09 '22 at 22:42
  • though if you plop out more buffer i.e. `echo 'x = '.$x.($x==1?'<!'.str_repeat('-', 100).'>':'').'';` it will probably work – Lawrence Cherone Jan 09 '22 at 22:45
  • I tried to change the echo line you suggested, but still is not working. I'm trying to perform the simplest example possible to see a "gradual output" in a php page. I will google for sse. Since for now I would like to skip websockets and anything that I consider to be "advanced", can you suggest me a simple example of clientside code that you said? – Riemann Jan 09 '22 at 23:01
  • see: https://stackoverflow.com/questions/10144780/how-do-echo-out-progress-on-mamp-using-flush/10145076#10145076 for example. basically your host expects more output then a few chars before sending the buffer if the script is still running – Lawrence Cherone Jan 09 '22 at 23:23
  • 2
    the simplest example possible https://playcode.io/851547/ – Lawrence Cherone Jan 09 '22 at 23:29
  • @LawrenceCherone the html works perfect! but I tried to run the code where you explained the ouput buffer and makes the same "error": you made a loop with 5 iterations, so my page will "freeze" for 5 seconds and then will put me 0 1 2 3 4 all at once! What makes me confused is that this works in local host flawlessly. Could it be my web hosting that is not suited for these tasks? What can make a local server and a real web hosting server so different? thanks for your hints btw – Riemann Jan 10 '22 at 00:02
  • @LawrenceCherone found it a first kind of solution php.net/manual/en/function.sleep.php#95164 .. from a comment in this question https://stackoverflow.com/questions/4191349/php-buffer-why-r-n. This is really fascinating I will dig in this – Riemann Jan 10 '22 at 00:12

1 Answers1

2

This is for sure not the ideal/optimal solution, but will output the increment of the variable inside the loop gradually.. I took this code from here and modified it.

When I used str_repeat(".", 4096); it gave me back 4 increment all at once, when I used str_repeat(".", 8000); it gave me back 2 increment all at once, so at the end I setted $buffer = str_repeat(".", 16384); and "printed" 1 increment everytime.

<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <title>Loop</title> 
   

    <?php   //https://stackoverflow.com/questions/10144780/how-do-echo-out-progress-on-mamp-using-flush/10145076#10145076
        ob_start();
        
        ob_implicit_flush(true);
        
        set_time_limit(0);


    $buffer = str_repeat(".", 16384);
    echo $buffer."\r\n<br />\r\n";

    for ($i=0; $i<20; $i++) {
        echo ($i+1).""."\r\n<br />\r\n".$buffer."\r\n<br />\r\n";
        ob_flush();
        flush();
        //sleep(1);
        usleep(500000);
    }


        ob_end_flush();
    ?>
  

</body>
</html>

I hope anyone can contribute with another solution

Riemann
  • 67
  • 9