The manual says that it flushes the output to a file. Plus they give an example.
$filename = 'bar.txt';
$file = fopen($filename, 'r+');
rewind($file);
fwrite($file, 'Foo');
fflush($file);
ftruncate($file, ftell($file));
fclose($file);
I tried to understand its necessity. What I did was the following:
fflush($file);
from the script and runned it again.frwite()
line to fwrite($file, 'Foo'); die();
Now, I don't see the point of using fflush()
.
I can't see the necessity of fflush in this example. Can you provide an example in which fflush()
is really necessary.
Link: http://php.net/manual/en/function.fflush.php
fflush
For efficiency purpose, core system library of PHP does not necessarily writes to a file immediately when we call functions like fwrite
or fputcsv
. It stores them in buffer and stores at the same time after a while.
For eg lets say you have loop and inside that loop you call fputcsv
function then system library of PHP does not necessarily stores it into the file at every loop.
fflush
?But for logging purpose we need to ensure that log is seen as soon as possible therefore it is better to use fflush
after each fwrite
. For other purpose than logging we don't need to force flushing. Let PHP internals handle the efficiency.
This function forces a write of all buffered output to the resource pointed to by the file handle.
fflush
PHP Documentaiton
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With