Reputation: 6010
Will I be ok doing this?
foreach ($item as $val)
{
include('external_script.php');
}
Where external script is about 800 lines of code I want to keep separate for organizational sakes.
Gracious!
Upvotes: 8
Views: 15650
Reputation: 17555
It will work but there's a disk I/O overhead for calling an external file in a loop unless you happen to have APC, XCache, eAccelerator running. Besides, you can't use include. You should be using include_once if it's the same file you're reloading
Upvotes: 5
Reputation: 6585
You wound not be killed by a god for doing that, and it would even work. But still function is better.
Upvotes: 2
Reputation: 6573
ermmmm - why?
if its the same file include it once - perhaps put the code in it in a function and just call that function how ever many times you need to.
Upvotes: 0
Reputation: 94153
Whether you will be okay or not depends on if you want to include your external script in each iteration or not.
Note that if your included file contains functions, you will end up with errors for trying to define the same function multiple times.
Upvotes: 1
Reputation: 655239
I guess you should better use a function for this.
Including a file requires to read, parse, and interpret the file. But if you have a function that you just feed with the current $item
, it its code is just read, parsed and interpreted once and you won’t have that overhead you would have with including.
Upvotes: 8