Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does PHP not output the page all at once?

Tags:

html

php

Here's something odd, I though this would output a page and show part by part, until all is loaded (similar to how Wordpress update/reinstall process works):

<html>
<body>
<?php
for( $i=0; $i<100; $i++)
{
    echo 'HELLO';
}
sleep(10);
echo '<p></p>';

for( $i=0; $i<100; $i++)
{
    echo 'THERE';
}
sleep(10);
echo '<p></p>';

for( $i=0; $i<100; $i++)
{
    echo 'HOW ';
}
sleep(10);
echo '<p></p>';


for( $i=0; $i<100; $i++)
{
    echo 'ARE U';
}
sleep(10);
echo '<p></p>';

Oddly enough it waits for the entire page, then shows it. What variables/configuration affect this behavior?

like image 683
NoBugs Avatar asked Dec 15 '22 06:12

NoBugs


1 Answers

I dont know how you think php works, but it is only server side and you are expecting partial updates in client side. PHP does not work like that.

When you call that php script, php interpreter begin to execute it, putting the output into temporary location, then with the sleep function, php interpreter waits until milliseconds are reached, then continue executing, concatenating the output in the temp location, and go on... after all script execution is done, php sends the whole output to the client.

If you need partial updates on a page, you need to enter in the world of async calls and ajax.

like image 156
Ivan Perales M. Avatar answered Dec 16 '22 20:12

Ivan Perales M.