PHP SplFileObject fflush() Function Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report The SplFileObject::fflush() function is an inbuilt function in the Standard PHP Library (SPL) in PHP that is used to flush the output buffer of the file. Syntax: public SplFileObject::fflush():boolParameter: This function does not accept any parameters. Return Value: The SplFileObject::fflush() function returns "true" if the function successfully flushes the output buffer of the file otherwise, this function will return "false". Program 1: The following program demonstrates the SplFileObject::flush() function. The "output.txt" should be available in the same folder. PHP <?php $file = new SplFileObject("output.txt", "w"); $data = "This is some data that will be written to the file.\n"; $file->fwrite($data); // Flush the data to the file immediately if ($file->fflush()) { echo "Data flushed successfully to the file."; } else { echo "Failed to flush data to the file."; } ?> Output: Data flushed successfully to the file.Program 2: The following program demonstrates SplFileObject::flush() function. The "output.txt" should be available in the same folder. PHP <?php $file = new SplFileObject("output.txt", "w"); $dataLines = [ "Line 1: This is the first line of data.\n", "Line 2: This is the second line of data.\n", "Line 3: This is the third line of data.\n", ]; foreach ($dataLines as $line) { // Write the line to the file $file->fwrite($line); // Flush the data to the file // immediately after writing each line if ($file->fflush()) { echo "Data flushed successfully for line: " . $line; } else { echo "Failed to flush data for line: " . $line; } } ?> Output: Data flushed successfully for line: Line 1: This is the first line of data.Data flushed successfully for line: Line 2: This is the second line of data.Data flushed successfully for line: Line 3: This is the third line of data.Reference: https://www.php.net/manual/en/splfileobject.fflush.php Comment More infoAdvertise with us Next Article PHP SplFileObject fflush() Function N neeraj3304 Follow Improve Article Tags : PHP PHP-file-handling PHP-function Similar Reads PHP | SplFileObject fputcsv() Function The SplFileObject fputcsv() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used write a field array as a CSV line. Syntax: string SplFileObject::fputcsv() Parameters: This function accept four parameters one is mandatory and three are optional. $fields: Specifies the a 1 min read PHP | SplFileObject fstat() Function The SplFileObject::fstat() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to give the information of the file. Syntax: array SplFileObject::fstat( void ) Parameters: This function does not accept any parameter. Return values: This function returns an array which c 1 min read PHP | SplFileObject ftell() Function The SplFileObject::ftell() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used to return the position of the file pointer which specifies the current offset in the file. Syntax: int SplFileObject::ftell( void ) Parameters: This function does not accept any parameters. 1 min read PHP | SplFileObject fgets() Function The SplFileObject::fgets() function is an inbuilt function of the Standard PHP Library (SPL) in PHP which is used to get a line from the file. Syntax: string SplFileObject::fgets( void ) Parameters: This function does not accept any parameter. Return values: This function returns a string contain 1 min read PHP | SplFileObject eof() Function The SplFileObject::eof() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used reached end of file. Syntax: string SplFileObject::eof( void ) Parameters: This function does not accept any parameter. Return values: Returns TRUE on Success. Below Programs illustrate the Sp 1 min read Like