how the PHP engine interacts with the Web server

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

how the PHP engine interacts with the Web server

Post by jasongr »

Hello

I have been wondering about some issue for some time now and I hope that someone will be able to clear it up for me.

When a PHP page is sent to be parsed and then its results are sent via the Web server to the client is streaming taking place?
Is the entire page first parsed generating a complete HTML page and only then is the page returned to the client?
Or maybe the PHP engine sending pieces of the code as soon as they are ready to the Web server. Meaning that some output is being sent to the client while the rest of the page is still being processed.

Here is a hypothetical example.
Assume I wrap the entire page with:

Code: Select all

ob_start();
<html>
... content here
</html>
$content = ob_get_contents();
ob_clean();
print $content;
Will that be a very inefficient in terms the time the client will have to wait for the reponse than simply writing:
<html>
... content here
</html>

?

thanks in advance
Skunkmaster
Forum Newbie
Posts: 1
Joined: Sun Jan 16, 2005 5:39 pm
Location: &#1500;&#1514;&#1493;&#1512; &#1502;&#1493;&#1496;&#1493;&#1512;

Post by Skunkmaster »

I guess that when you are using
ob_start(); for the buffer
and the file has a php extention so the php cgi is handling the file
before it being sent to the client so the whole buffer will be flushed when the entire script will end and not before
HTH
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

if output buffering is off, i think php constantly writes the output back to the webserver. for example every echo gets written to the webserver immediately.


you can set a size limit for output buffering


like for example, if you set the limit to 1024, php will buffer 1024 bytes of data before flushing it to the webserver. if you dont set a value, it will just buffer it all until either the script ends, or you flush the buffer manually or end output buffering.


btw- in your example, you would want to use ob_end_clean();
ob_clean just clears the buffer, but doesnt turn output buffering off. so on the next line where you print the output, it gets buffered again.

also, in your example, you could achieve the same effect by just putting ob_start() at the top, no need for ob_get_contents();
php will automatically flush the output buffer when the script ends.


its hard to say whats more efficient, because it will vary. i know ive heard output buffering can improve performance by reducing the amount of writes to the output buffer(like if you have tons of small echo statements), and making sure each packet sent to the client is as large as possible, but if you have a huge amopunt of html being output, the user cannot even begin downloading until your entire script is finsihed.

so to me it sounds like using output buffering, but setting a limit like 4096 would prob give good overall performance.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the actual parsing out of information to the client is dependant on the server, mostly. Apache buffers a bit before sending data. If you check the headers sent by the page to the browser, you'll often see "chunked", which can hint at the server buffering data. Since TCP/IP packets have a minimum size, it's best to pack as much data into the packet as possible, within a period of time. Otherwise, you are making the user download more, rather than less.
Post Reply