I am trying to print a UDP message in a while loop which runs 10 times in PHP. the problem is that it waits until the loop completes and then the message is printed 10 times but I want to print it on each iteration of the loop not at the completion of the loop. Here is my code.
<?php
$socket = socket_create(AF_INET,SOCK_DGRAM,SOL_UDP);
socket_bind($socket,$ip_address,$port);
$i = 0;
while($i < 10)
{
$i++;
$FROM = '';
$PORT = 0;
socket_recvfrom($socket,$buffer,1024,0,$from,$PORT);
echo "Receiving $buffer from IP $FROM and Port $PORT", PHP_EOL);
flush();
sleep(1);
}
?>
Call ob_flush() to flush the output buffer, after your call to flush().
If accessing from a browser,
header( 'Content-type: text/html; charset=utf-8' );might need to be added at the top of your webpageAlso an ob_start() at the beginning of the script turns on output buffering.