Friday, October 10, 2025
HomeLanguagesPHP fflush( ) Function

PHP fflush( ) Function

The fflush() function in PHP is an inbuilt function which is used to write all the buffered output to an open file. The fflush() function forces a write of all buffered output to the resource pointed to by the file handle. The fflush() function returns true on success and false on failure.

Syntax:

fflush($file)

Parameters: The fflush() function in PHP accepts only one parameter which is $file. It specifies the open file stream.

Return Value: It returns TRUE on success and FALSE on failure.

Errors And Exception:

  1. The fflush() function results in errors if the file pointer is not valid.
  2. The file pointed must be opened by fopen() or fsockopen() and closed by fclose().

Below programs illustrate the fflush() function.

Program 1: In the below program the file named singleline.txt contains a single line of information which is “This file consists of a single line.”.




<?php
  
// The file is opened using fopen() function
$check = fopen("singleline.txt", "r");
$seq = fgets($check);
  
// Writing buffered output to a file
// until the end-of-file is reached
while(! feof($check))
    fflush($check);
  
// The file is closed using fclose() function
fclose($check);
  
?>


Output:

This file consists of a single line.

Program 2: In the below program the file named gfg.txt contains the following piece of text.

This is the first line.
This is the second line.
This is the third line.




<?php
  
// The file is opened using fopen() function
$check = fopen("gfg.txt", "r");
$seq = fgets($check);
  
// Writing buffered output to a file
// until the end-of-file is reached
while(! feof($check))
    fflush($check);
  
// The file is closed using fclose() function
fclose($check);
  
?>


Output:

This is the first line.
This is the second line.
This is the third line.

Reference:
http://php.net/manual/en/function.fflush.php

RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7099 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS