What are the reasons for using a function in PHP? I can see no true advantage. Is it faster? In server side / executed on page load honestly I see no purpose, that might just be me though.
Please explain a useful application.
Thanks,
Tim
What are the reasons for using a function in PHP? I can see no true advantage. Is it faster? In server side / executed on page load honestly I see no purpose, that might just be me though.
Please explain a useful application.
Thanks,
Tim
I don't understand your question.
"Is it faster?"--
That entirely depends on the context. Functions are slightly slower because there is an extra step (calling the function, going to the function*), but can sometimes save time depending on how they are coded.
Functions certainly save time editing and manipulating code. If functions did not exist, php would be so very limited that basically all you could do is echo text and include files-- everything else-- the things like this()-- is a function. I assume you are talking about custom functions, and the same applies-- you can expand the set of "default operations" by creating a function and things are more organized and easier to code, with less text and less time from the programmer. In some cases there is no real way around it aside from creating functions, or maybe a really long way that takes a lot more code.
The idea of a function is like a function in math: it is a stored operation that can be applied to new situations: add($a,$b) { return $a+$b; }
Very basic example, but now there is a function to add $a to $b and get the answer.
If you expand that to something more complex, it will save you from having to write it each line. Of course you could write it each time, but that is wasted effort.
Functions within PHP are the equivalent to using PHP in the first place compared to just plain old text files-- why would you want to change a lot of instances of the same code each time you want to do something instead of just borrowing from a function to repeat it?
Functions also organize things in a nice human-readable way, rather than a lot of commands that start to blur together. Functions can be named logically, rather than code that always looks confusing:
function dothemaththing(....) { ........ }
From that, you can "do the math thing" without actually thinking about any more what that math is, and not having to look at it-- you've stored the operation and can call it any time you'd like. And if you ever need to change the "math thing" you can just do it inside that one function, and all of the times you use it later will automatically be linked to it-- so you don't need to update each.
If you aren't using functions now, you should be, and once you do you'll like them (and wonder how you programmed before using them).
(*This is so minor it's irrelevant, unless you are doing a loop of at least 1,000 times and calling a function each time-- then it would create a SLIGHT delay-- but of course looping through the text 1,000 times to parse it would slow down the original parse of the code so it would not make things faster.)
Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum
twQ (01-25-2010)
I guess I just don't do enough with PHP to find functions very useful.
My view is to access PHP I have to do some form of request since it's not browser based. Ajax, a new HTTP request whatever. So if I have to do that anyways I've never found much I couldn't do with if / else or while.
I'm sure I'll find a good use eventually, I was just curious if in my basic scripts (where I'm not using functions) I should have been.
Thanks for the great answer.
Fassist
Well, different purposes. PHP can create great interactive features, but like you said, it's server-side. That means stuff like searching your site DB, etc., is where you'll get the most use from it. Also consider that PHP's main purpose is to write HTML:
If you had hundreds of images and/or changed them frequently, this function would save you tons of time - especially together with a function that found the image paths dynamically.PHP Code:// $imageNames is an array of file paths for your images
// it could be declared manually or dynamically by another function such as scandir()
function writeIMGtags($imageNames){
foreach($imageNames as $img){
echo '<img src="/images/'.$img.'" class="image" alt="dynamically generated image tag">';
}
}
Bookmarks