What is the best practice for including external files in a loop in PHP?

When including external files in a loop in PHP, it is important to consider performance implications. Repeatedly including files within a loop can lead to unnecessary overhead and slow down the script execution. To address this issue, it is recommended to include the external file outside of the loop and pass any necessary variables as parameters to the included file within the loop.

// Include the external file outside of the loop
include 'external_file.php';

// Loop through data and pass variables to the included file
foreach ($data as $item) {
    include 'external_file.php';
    // Pass variables to included file
    $variable1 = $item['variable1'];
    $variable2 = $item['variable2'];
}