Recently someone asked me a simple question: When do I use a function as opposed to just repeating the code. It's a good question that I'll try to answer as we go along. The answer applies to all programming and scripting languages but I'll be focusing on PHP because that's the only language I'm using right now.
people who create functions when functions aren't necessary suffer from what I like to call "functionitis". Programmers back in the 80's did the same thing with subroutines. Less time is spent on optimization than making the code readable. What some PHP programmers fail to understand is that avoiding optimization leads to bloated code, and that translates to larger pages which take longer to download. A large percentage of people today still connect to the Internet by dial-up modem. Every fraction of a second counts for those people.
I'm constantly downloading scripts of different kinds in order to examine techniques and snippets of code because I'm always trying to learn. In the process, I've noticed that some PHP programmers create functions for routines that they only use once while others seem to repeat the code over and over. Here are a couple of basic rules:
1) Never write a function for code that will only be used once. If you wrote a function because you intended to use it more than once, and when you finished you found out that you only used it once, go back and reintegrate it back into the main script and get rid of the function.
2) Never repeat a routine. The only exception to this rule is when repeating it once or twice requires less overhead than a function. In a case like this, a non-function "include" would probably be more appropriate.
I personally don't like creating functions. While they make coding easier, they really don't speed up the script or make the script shorter. I prefer to use includes and introduce redundant code as needed using conditional statements.
Update 2007-07-28: Read the comments. This was a hoax.



