PHP Functions

Mun

Never Forget
I am currently building a new project, and as I sit there looking at my blank screen I just have to ask, why do we use functions? Should we use functions? What are there advantages against just running the code straight through?

Anyways post your thoughts below :)
 

Aldryic C'boas

The Pony
Repetition. If you're going to be performing a similar task over and over, having functions will save you time and add coherency to your code.


Updates. Changing how something works? Modify the function to match up, rather than have to update large portions of code.


Security. Passing data to a DB? Collecting user input? Functions can be written to sanitize data without having to put preg_replace all over your script.


Sanity. Ever go back and look at code you wrote months ago and haven't had to touch since? Ever sat there and wondered exactly what the hell you were trying to accomplish? Functions (and proper commentary) can make rarely-edited scripts much easier to comprehend.


Efficiency. Relatively minor, unless you happen to be writing some monster project. But a function only has to be 'read' once and used over and over, as opposed to having to 'read' long batches of raw code.
 
Last edited by a moderator:

peterw

New Member
I use functions to build small libs. One for mails, one for http communication, one for files, etc. Don't do things twice.
 

Damian

New Member
Verified Provider
When you say "functions", do you mean object-oriented programming, or procedural programming? I can't think of anything more than an extremely basic script that wouldn't use functions.

If you mean "why do we use OOP instead of procedural", http://stackoverflow.com/questions/716412/why-use-php-oop-over-basic-functions-and-when and http://stackoverflow.com/questions/6480676/why-use-classes-instead-of-functions have some good examples to visualize, with http://stackoverflow.com/questions/24270/whats-the-point-of-oop being a fun counterpoint.
 

BlackoutIsHere

New Member
Verified Provider
Repetition. If you're going to be performing a similar task over and over, having functions will save you time and add coherency to your code.


Updates. Changing how something works? Modify the function to match up, rather than have to update large portions of code.


Security. Passing data to a DB? Collecting user input? Functions can be written to sanitize data without having to put preg_replace all over your script.


Sanity. Ever go back and look at code you wrote months ago and haven't had to touch since? Ever sat there and wondered exactly what the hell you were trying to accomplish? Functions (and proper commentary) can make rarely-edited scripts much easier to comprehend.


Efficiency. Relatively minor, unless you happen to be writing some monster project. But a function only has to be 'read' once and used over and over, as opposed to having to 'read' long batches of raw code.
 Great examples! Even with short code I tend to use functions especially when they need to be nested.
 

NodeBytes

Dedi Addict
Like others have said, functions save a ton of time and they can help you out with developing large projects where repetition occurs. I tend to use a lot of functions in my programming because they can be reused in different projects. I've built functions for a project and then gone back and was able to use it again for another project.

Object oriented programming allows you to be much more flexible in your style of programming and use an agile development scheme which for me means being able to have a somewhat functional project that you can be testing as you go through. Using functions is a big part of this because it gives you flexibility throughout your project and allows you to develop quicker and smarter and in a lot of cases more securely because you are cleaning up as you go.

Just my $0.02, others may disagree.
 
Last edited by a moderator:
Top