8

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!

2
  • 5
    ........ So, I'm maintaining a 30,000 line pile of slop where the programmer does this all the time. If you are him, may I punch you through the internet? Commented Aug 6, 2010 at 16:23
  • @Incognito, You mean 30k lines in a single file or do you mean a total of 30k lines? Commented Mar 6, 2015 at 9:15

5 Answers 5

8

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.

Sign up to request clarification or add additional context in comments.

5 Comments

A problem I see with making it a function would be having to pass variables into the function, and there are 80+ variables that the LOC use.
@atwellpub: Are these all variables that are defined somewhere before?
Encapsulate all of those variables in a class? It definitely sounds like there are some design problems here.
@atwellpub the more details coming, the worse it seems. why not to tell us the whole story at once, instead of feedeng by small chunks?
I would need to learn how to encapsulate variables in a class, then a function sounds to be the best solution. Will there be any speed difference between a function and include_once?
5

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

2 Comments

In a certain situation on host ipage.com, doing this gives me a 500 server error.
Lots of things can cause a 500 server error. Maybe you should start a new Q&A.
2

You wound not be killed by a god for doing that, and it would even work. But still function is better.

Comments

1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.