R
R
rkfddf2021-08-09 11:13:38
PHP
rkfddf, 2021-08-09 11:13:38

How to display data immediately in php?

If you run this code in php

<?php 
for ($i = 0; $i< 3; $i++)
{
    echo $i. '<br>';
    sleep(1);
}

then the result will be displayed after the end of the entire script, that is, the numbers 0, 1, 2 will be displayed simultaneously after three seconds, that is, after all iterations have been completed.
If the same code is written in python, then 0 will be displayed, after a second 1 will be displayed, and so on.
Is there a way in php to execute python-style programs without having to wait for the entire script to complete?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Slava Rozhnev, 2021-08-09
@rkfddf

Output control functions

<?php
header( 'Content-type: text/html; charset=utf-8' );
echo 'Begin ...<br />';
for( $i = 0 ; $i < 10 ; $i++ )
{
    echo $i . '<br />';
    flush();
    ob_flush();
    sleep(1);
}
echo 'End ...<br />';
?>

V
Vitsliputsli, 2021-08-09
@Vitsliputsli

What you want doesn't need to be done over http, either use web-socket or move this functionality to the front entirely. Http is designed to receive data, not send signal commands from the server. Manipulations with ajax or buffer overflows will also be a perversion.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question