當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


PHP SplFileObject fflush()用法及代碼示例


SplFileObject::fflush()function 是 PHP 標準 PHP 庫 (SPL) 中的內置函數,用於刷新文件的輸出緩衝區。

用法:

public SplFileObject::fflush():bool

範圍:該函數不接受任何參數。

返回值: SplFileObject::fflush()如果函數成功刷新文件的輸出緩衝區,則函數返回“true”,否則,該函數將返回“false”。

程序1:下麵的程序演示了SplFileObject::flush()函數。 “output.txt” 應位於同一文件夾中。

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."; 
} 
  
?>

輸出:

Data flushed successfully to the file.

程序2:下麵的程序演示了SplFileObject::flush()函數。 “output.txt” 應位於同一文件夾中。

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; 
    } 
} 
  
?>

輸出:

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.

參考:https://www.php.net/manual/en/splfileobject.fflush.php



相關用法


注:本文由純淨天空篩選整理自neeraj3304大神的英文原創作品 PHP SplFileObject fflush() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。