views:

91

answers:

2

By functions file I'm referring to a file that is called for in each PHP page and contains functions. I noticed another question asking about function files and it reminded me that my PHP website did not use any function file. So I'm just wondering if function files are recommended for sites.

A: 

If the concept of functions is not familiar to you, they're essentially re-usable blocks of code. If you have a bunch of functions you created, you probably want to re-use them here and there throughout your site. So what you do is collect them in one central file (or several grouped by "topic") and include them in every page you need them.

// In file "often_used_functions.php"
function foo () { ... }
function bar () { ... }
function reticulate_splines () { ... }

---------------------------------------

// In some other file:
include "often_used_functions.php";

foo();

It's all to make code re-usable.
I.e. if you find yourself writing basically the same code again and again with only slight variations, put that code into a function and put that function into some central, re-usable file.

deceze
+1  A: 

You use a "functions file" to help prevent unnecessary code repetition. If you have a function that you will use in more than one place you place it in a functions / library / lib / your-coolname.php to be included on the pages that need access to it.

wlashell