PHP: Output data before and after sleep()?
faileN's answer is correct in theory. Without the ob_flush()
the data would stay in PHP's buffer and not arrive at the browser until the buffer is implicitly flushed at the end of the request.
The reason why it still doesn't work is because the browsers also contain buffers. The data is now sent out correctly, but the browser waits after getting "one" before it actually kicks off rendering. Otherwise, with slow connections, page rendering would be really, really slow.
The workaround (to illustrate that it's working correctly) is, of course, to send a lot of data at once (maybe some huge html comment or something) or to use a tool like curl on the command line.
If you want to use this sending/sleeping cycle for some status update UI on the client, you'd have to find another way (like long-polling and AJAX)
PHP output text before sleep
check this out
<?php
ob_start();
echo 'Output one.';
ob_flush();
usleep(1500000);
echo 'Output two.';
ob_flush();
?>
PHP AJAX echo json data before then sleep
Most likely the echo'd data is just being buffered rather than sent until sleep finishes and the request completes. Try using flush()
to force pushing the output to the client:
echo json_encode($return);
flush();
sleep(7);
die();
If this fails, you may find padding the output will help meet the server/browser's minimum length requirement to flush/display the data:
echo str_pad(json_encode($return),8192," ");
flush();
sleep(7);
die();
Related Topics
How to Call Entity Manager in a Constructor
PHP - How to Know If Server Allows Shell_Exec
How to Send a File via Axios to Laravel
How to Run PHP Code from File_Get_Contents or File in a Function
How to Select 10 Random Things from a List in PHP
Elegant Way to Search an PHP Array Using a User-Defined Function
How to Set Base Url for All Pages of My Website
Sort Array by Date in Descending Order by Date in PHP
Invalid Sos Parameters for Sequential Jpeg
Model->Save() Not Working in Yii2
MySQL Password Hashing Method Old VS New
Switch of PHP Versions Not Working on MAC
How to Start Local Server with Symfony 5 or Downgrade Version to 4.4
Mysql: Get Total in Last Row of MySQL Result
Woocommerce: Display Also Product Variation Description on Cart Items