PHP Timer usage - php

Firstly, I want to echo text and ,after 5 seconds,I want to echo another text.
So I write...
<?php
echo 'Text';
flush();
sleep(5);
flush();
echo 'Another Text';
?>
But I see both of (Text Another Text ) after 5 seconds.How can I do for this?

From documentation
Several servers, especially on Win32, will still buffer the output from your script until it terminates before transmitting the results to the browser.
So you see both TextAnother Texttogether.

Take a look at output buffering.

Have you started output buffering at some time? If so, you need to use ob_end_flush() to flush.

Switch off output buffering if you have it on or call ob_flush() too. Any output compressing (like gzip) will also mess it up. If it doesn't work try modifying these two php.ini directives to:
output_buffering = off
zlib.output_compression = off

Related

output buffering ob_get_clean not working

I have the code below code that works correctly as what i want. It does not send any output on browser that is what i exactly want.
ob_start();
echo "test";
echo "test";
$output = ob_get_clean( );
But problem is in my below code. below code start sending the output on the browser even i have $output = ob_get_clean( ); at the end
ob_start();
for($i=0;$i<=10000000;$i++){
echo $i."<br/>";
}
$output = ob_get_clean( );
I am unable to understand the concept of output buffering. everyone say you can control the output and send the output when you want but my above script start sending output to the browser.
There's a limit to how much there can be buffered which by default is 4KB so you are hitting the maximum with your script. If you want to use it for a larger buffer you have to edit your php.ini settings to reflect that. To quote from the PHP docs
You can enable output buffering for all files by setting this directive to ‘On’. If you wish to limit the size of the buffer to a certain size – you can use a maximum number of bytes instead of ‘On’, as a value for this directive (e.g., output_buffering=4096). As of PHP 4.3.5, this directive is always Off in PHP-CLI. source
The PHP doc is quite technical on the subject so I found this blog post which explains the bits and tricks to output buffering:
Streaming and Output Buffering

What is the use of the ob_get_length() method?

i know those methods related to output buffering:
<?php
ob_start();
// some code
ob_end_flush();
ob_end_clean();
but i can't figure out why will i need the length of the output buffer. there is one question here is SOF about measuring the bandwidth of the user ,although that calculation will need to measure pictures and other files which is not addressed.
i did check the PHP manual and the "why" is not there.
Answering "why to use it". In order to answer this, we must first understand why we need to use ob_start.
Usually it is used for two purpose:
When we want to send HTTP headers, but we are not sure, that there is no rendering before(the first output will trigger header sending, so if you use, echo for example, before header, the later won't work)
When we want to send our output by chunks for performance. (your code can be written with a lot of echo or print, and usually they outputing only small data, so you are sending small portions of data back to browser, which is affecting performance.)
So now you can start to guess, why we need to use ob_get_length(). For example: i want to send my output by large chunks, so i am enabling output buffering and with the help of ob_get_lengthi can control how many data i can write to buffer, before sending it.
ob_get_length will return the length of the contents in the output buffer..
Here is simple example..
<?php
ob_start();
echo "Hello ";
$len1 = ob_get_length();
echo "World";
$len2 = ob_get_length();
ob_end_clean();
echo $len1 . ", ." . $len2;
?>
The above example will output: 6, 11
Updated:
ob_get_length() is helpful if you want to send a custom HTTP Content-Length header - although that is for advanced users only!

Save Output of PHP Virtual() to String

Is there a way to get the Output from PHP Virtual()?
ob_start()
virtual();
ob_end_flush();
is in my case not working.
Per the manual http://php.net/manual/en/function.virtual.php:
To run the sub-request, all buffers are terminated and flushed to the
browser, pending headers are sent too.
One way that I can think of is to create a separate file and load that:
// virtual.php
virtual('/path/to/whatever');
Then wherever you want to get the contents, load it up:
// other.php
$string = file_get_contents('http://www.example.com/path/to/virtual.php');
You use obj_end_flush() which stops output buffering and deletes buffered output.
What you would probably want is this:
ob_start();
virtual();
$output = ob_get_clean();
$output contains the buffered output because ob_get_clean() stops output buffering and returns the buffered output instead of deleting it.
PHP manual ob_end_flush()
PHP manual ob_get_clean()

What is advantages of using ob_start() over don't using it?

What is advantages of using ob_start()? What is its effects on performance? Say i have this code:
echo 'hello';
echo 'world';
compare to:
ob_start();
echo 'hello';
echo 'world';
ob_end_flush();
Which one has best performance and why?
The effect on performance is negligible.
Normally PHP renders line by line as instructions are executed. however once you have output buffering turned on by using ob_start() it means that php will buffer the output and not render it until you hit ob_end_flush()
This is used in case you need to do more processing before sending down the output to the client.
However...
Although output buffering does not affect the performance, you can use it cleverly to increase your website performance. have a look at here

ob_flush takes long time to be executed

In my website(running with drupal) the ob_flush function takes a long time(between 10 - 100 secs) to be executed. How do I find out why? What can cause this so long time?
Try this:
ob_start();
//Your code to generate the output
$result = ob_get_contents(); //save the contents of output buffer to a string
ob_end_clean();
echo $result;
It is run quick for me.
[You may want to tag your question with Drupal, since this feels like it might be a Drupal issue. Specifically, I suspect that when you flush the buffer, you're writing to an outer buffer, which triggers a ton of hooks to be called to filter the data you've just written.]
I suspect that your problem is nested buffers. Drupal really likes buffers and buffers everything all over the place. Check the result of:
echo "<pre>\nBuffering level: ";
. ob_get_level() .
. "\nBuffer status:\n"
. var_dump(ob_get_status(TRUE))
. "\n</pre>";
If you've got nested buffers, then I suspect ob_flush() will do nothing for you: it just appends the contents of your inner buffer into the next outermost layer of buffering.
Nested buffers can come from Drupal itself (which the above will show), or from the settings for zlib-output-compression and output_buffering (try twiddling those, see if it changes anything).
If your buffers are not nested, and the above settings do not help, then you may also want to split the operation into pieces, and run the profiler there, to see which part is taking the time:
$data = ob_get_contents(); // Return the contents of the output buffer.
ob_clean(); // Clean (erase) the output buffer.
ob_end(); // Close the buffer.
echo($data); // Output our data - only works if there's no outer buffer!
ob_start(); // Start our buffer again.
The question then becomes, though, "what are you trying to accomplish?" What do you think ob_flush() is doing here? Because if the answer is "I want to push everything I've done so far to the browser"... then I'm afraid that ob_flush() just isn't the right way.
SET
output_buffering = Off
in php.ini
use
<?ob_start();?>
at the beginning of the page and
<?ob_flush();?>
at the end of the page, to solve this problem.

Categories