How many lines of PHP code is too many for one file?

Marcus picture Marcus · Dec 28, 2009 · Viewed 10k times · Source

I'm creating a PHP file that does 2 mysql database calls and the rest of the script is if statements for things like file_exists and other simple variables. I have about 2000 lines of code in this file so far.

Is it better practice to include a separate file if a statement is true; or simply type the code directly in the if statement itself?

Is their a maximum number of lines of code for a single file that should be adhered to with PHP?

Answer

Kel picture Kel · Dec 28, 2009

I would say there should not be any performance issue related to the number of lines in your php files, it can be as big as you need.

Now, for the patterns and best practices, I would say that you have to judge by yourself, I saw many well organized files of several thousand lines and a lot of actually small and difficult to read files. My advise would be:

  • Judge the readability of the source code, always organize it well.
  • It's important to have a logical separation to some extent, if your file does both: heavy database access, writing, modification, html rendering, ajax and so on.. You may want to separate things or use object oriented approach.
  • Always search the balance between the logical separation and code. It should not be messy nor extra-neat with a lot of 10-line files