One File or Multiple Files?

Lets say i’m going to use PHP strictly to process data. The user gives a query like “13”, which filters through 20 possible switch statements, processes a function, and spits out a numeric response.

But lets say that since some functions are really long and some are short, the entire file is a megabyte (which is extreme, of course).

Here are my questions.

  1. If the file is a megabyte, but that’s all on server side, the user doens’t have to download the entire file to view the result, right?
  2. If the file is something like 200 kilobytes, does it take it very much longer for the server to process a response through it if most of that code isnt being used on any given query?
  3. Would it be significantly slower to have 20 functions called from a switch statement, or would that be basically the same as having 20 different files? If it was possible for me to direct people to a different page based on the function they wanted (i.e. instead of ‘input function and hit submit’ have something like ‘click here for function 13, click HERE for function 14’).

Thanks in advance!

Do you have a zombie problem and nowhere else to turn? Ask the specialists!

Think of the switch command like multiple if statements. If this is true then process the below, if the next statement is true process this statement. The difference though, I beleive more than once switch statement can be true, unless you excape from the switch.
Since the script is processed server side, only what is true will be sent to the client side.

My website


Note that while your switch() will only call the function needed, ALL the functions will be loaded with the file, whether they’re called or not, if they exist in the file. This is unneeded overhead.

If you have a file of say 200k, consider a redesign of the application. You could still use the switch() method…but instead, separate out your functions into separate, logical files. This way, you can then simply include the files (er, functions) that are needed at the given moment without extra overhead.


  • Alar

That was the answer I was looking for. I figured PHP probably worked something like C, where every function, even unused ones, get called. I liked being able to add multiple functions in, because i didn’t have to have functions redundant in different files. But this will work fine too.

Do you have a zombie problem and nowhere else to turn? Ask the specialists!

To clarify …

The functions don’t get called automatically at any point…what I meant was that PHP has to parse through the entire file’s footprint before it can do anything…and thus, having extra functions (read: lines of code) in the file adds to it’s weight.

Splitting your functions into files will greatly reduce the time it takes to manage and extend your code later. Not to say that every function needs a file of it’s own, though… I usually create an API (or class) that’s accessed by /most of/ an application’s code … and then create sub-APIs so to speak, to be require()d where needed.

The savings is pretty good …