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 test but the result is always showed after the script executes. Is it a browser-side thing too?
问题:
回答1:
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.
回答2:
yes you can do that this way
<?php
echo "hello senad";
flush();
sleep(20);
echo "meskin";
?>
回答3:
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.
回答4:
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);
}
回答5:
PHP loads a page all at once then displays it. Use flush()
to show output as the script loads.
回答6:
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.