PHP include file in foreach Loop

Will I be ok to do this?

foreach ($item as $val) { include('external_script.php'); } 

If the external script is about 800 lines of code, I want to keep separate for the organization.

Gracious!

+7
php
source share
6 answers

It will work, but there is overhead of disk I / O for calling an external file in a loop, unless you have APC, XCache, eAccelerator. In addition, you cannot use include. You should use include_once if it is the same file that you reload

+4
source share

I think you better use the function for this.

Including a file requires reading, parsing, and interpreting the file. But if you have a function that you just pass with the current $item , then its code is just read, parsed and interpreted once, and you won’t have the overhead that you would have with inclusion.

+7
source share

Whether you will be fine or not depends on whether you want to include an external script at each iteration or not.

Note that if your included file contains functions, you will get errors trying to determine the same function several times.

+1
source share

You are injured so that you are not killed by God for this, and he will even work. But still the function is better.

+1
source share

ermmmm - why?

if its one and the same file includes it once - maybe put the code in the function into it and just call this function how many times you need.

0
source share

I think you should use the eval() function.

-5
source share

All Articles