What are the best practices for optimizing multiple fwrite calls in PHP code?
When optimizing multiple fwrite calls in PHP code, it is best to minimize the number of calls by concatenating the data to be written and then writing it in a single fwrite call. This reduces the overhead of opening and closing the file handle multiple times, resulting in improved performance.
// Example of optimizing multiple fwrite calls
$data = "First line of data\n";
$data .= "Second line of data\n";
$data .= "Third line of data\n";
$filename = "output.txt";
$file = fopen($filename, "w");
fwrite($file, $data);
fclose($file);