Forum Moderators: coopster
I built my site a few months ago and didn't use any functions at all :S. Now I am starting to realize how functions could help me.
Basically the way my site works is I have a file which calls all my includes for connecting to the db and functions which I am creating. I was curious if it would be better to have the code on seperate pages and not in includes if I would only use that bit of code once. If I would use it more I would put it in a function, but if I am only using it once, should I put it in a function? If so, if I added a bunch of functions to a file would it not slow my site down completely because that function file is called every time a page is loaded so would it not slow it down?
Thanks for your help :)
Wes
They make your code less cluttered, since you can group actions into modular processes like subroutines (in case you can remember the glory days of GOTO and GOSUB), and they also make your code much easier to debug.
Everyone has their own favourite way to stay organized. Personally, I create a custom function whenever
1) the code will be reused in more than one context
2) it's a long tricky process that only accomplishes one thing (then I use a function like it's a subroutine)
3) any kind of recursion is needed (very important)
I also have a habit of keeping all my functions in separate files, and including or "require_once"ing them as needed into a script. For instance, the function juggle_breadcrumbs() is in a file called "function_juggle_breadcrumbs.php". When I want to use that function, I make sure it's included at the top of the script.
One site I work on uses a template with many fancy things going on in the header. Since I have all my functions in separate files, I need to include them. The beginning of my header.php file looks like this:
"header.php"
------------
include("function_mysql_conn.php");
include("function_mysql_dbquery.php");
include("function_create_menu.php");
include("function_start_scroller.php");
include("function_flash_filter.php");
include("function_break_from_frames.php");
include("function_add_gototop_link.php");
...
And if I need that one special function just on a single page, I include it outside of the header.
INDEX.PHP
---------
include("header.php");
include("function_juggle_breadcrumbs.php");
<HTML>
<HEAD>
...
I have known programmers who build a library of ALL their favourite functions, needed or not, and keep them all in a big nested wad which they include at the start of every page they create. Need a "shuffle_cards()" function? It's in there. So is "answer_the_phone()" and "sort_my_vinyl_collection()".
I don't know how much this impacts performance (it must, right?), but I find the idea of it messy and annoying. I load up functions only when I need them. If I try to use a function and it's not there, I get an error, so I include() it.
I don't know if I would make a php file for each function, but I too wouldn't want to load a function that wouldn't be used :S I would think that it would slow your site down, maybe not a noticeable decrease but if you get a bunch doing that it would probably be noticable.
In general, functions slow down the code a tiny bit, but the advantages (development speed and stability) hugely outweigh the disdvantages. So you lose 1 microsecond, but you have a handful of pages that routinely fail completely.
Tom