How can the efficiency and readability of PHP code be enhanced when handling file creation and incrementing variables within loops?

When handling file creation and incrementing variables within loops in PHP, it is important to optimize the code for efficiency and readability. One way to do this is by using meaningful variable names, breaking down complex operations into smaller functions, and minimizing unnecessary file operations within loops.

// Example of creating files and incrementing variables within a loop
$baseDir = 'files/';
$numFiles = 10;

for ($i = 1; $i <= $numFiles; $i++) {
    $fileName = $baseDir . 'file_' . $i . '.txt';
    
    // Create a new file
    $file = fopen($fileName, 'w');
    fwrite($file, 'This is file ' . $i);
    fclose($file);
    
    // Increment a variable
    $incrementedValue = $i * 2;
    
    echo "File created: $fileName, Incremented Value: $incrementedValue\n";
}