echo from php “as it goes”

前端 未结 6 1773
耶瑟儿~
耶瑟儿~ 2021-01-20 18:27

what was the way to print results from a php script while it\'s printing stuff before the script ends? i tried to play with output buffer, putting sleep() between echos to t

相关标签:
6条回答
  • 2021-01-20 18:49

    Nice question.. when I want to do something like that (like loggin actions) I just use AJAX. And I know it's not probably what you wanted but hear me out.. I have had this problem as well 4 times so far and I've used AJAX and because of that I was able to put a preloader (which is really cool and helpful :))

    By "use ajax" I mean if you have 4 actions to show up do 4 ajax requests. I know it's not the most elegant solution (as you do a lot of extra stuff) but it is a good design-wise solution.

    0 讨论(0)
  • 2021-01-20 18:57

    All you could do in your PHP script to try to flush all the current output to the user's browser is a call to ob_flush, but it actually depends on many other things as well.

    The HTTP server has its own buffering, and the browser may not instantly render every packet it receives. It's like a long pipeline of buffers at different levels that all have to be flushed before you see anything in the browser, and the PHP output buffer is at the highest level.

    0 讨论(0)
  • 2021-01-20 18:57

    PHP is server side. If you are making a web page, then the results will ALWAYS be shown on the client after the script ends and has been transmitted.

    If you are running the script on your own computer, then I heard somewhere that one prints during execution and the other after the script is done. Try switching between print and echo and see how your results go.

    0 讨论(0)
  • 2021-01-20 18:57

    PHP loads a page all at once then displays it. Use flush() to show output as the script loads.

    0 讨论(0)
  • 2021-01-20 18:59

    There are 2 solutions:

    1) Deactivate the output_buffering in php.ini

    2) When using loops use this for example:

    for ($i = 1; $i <= 3; $i++) {
      echo md5(rand())."<br />";
      flush(); 
      ob_end_flush();     
      sleep(1);
    }
    
    0 讨论(0)
  • 2021-01-20 19:01

    yes you can do that this way

    <?php
     echo "hello senad";
     flush();
     sleep(20);
     echo "meskin";
    ?>
    
    0 讨论(0)
提交回复
热议问题