Reply
Putting together a function
Old 01-31-2005, 11:11 AM Putting together a function
fambi's Avatar
Ultra Talker

Posts: 339
Hello everyone.

I am new to functions, and don't uderstand entirely how they work.

I have this repeated in my code many times

$categorya = ucwords(str_replace("_", " ", $category));

How do i make a function that does that (and therefore take it out of my codes)?

Would it be advised?
__________________
Sending sms from a website or application is easy!
Read this great tutorial that uses our bulk sms gateway.
fambi is offline
Reply With Quote
View Public Profile Visit fambi's homepage!
 
When You Register, These Ads Go Away!
Old 01-31-2005, 11:17 AM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Think of functions as a repeatable action which can be taken and a result returned. A function here is totally appropriate because you can encapsulate your functionality. Here is the definition of your function (you can rename it whatever you want).


PHP Code:
function cleanWords$category )
{
    return  
ucwords(str_replace("_"" "$category));

Then in your code, you simply call the function:

PHP Code:
$category cleanWords$category ); 
The value returned by the function is assigned to $category. You already know how to use functions: str_replace() is a function whose result is submitted to the function ucwords() and assigned to a variable.
__________________
—Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 01-31-2005, 11:20 AM
fambi's Avatar
Ultra Talker

Posts: 339
Well explained. THat made sense to me without needing to read it twice.

I heard somewhere that a functions are slow. Any opinions with regards to that.
__________________
Sending sms from a website or application is easy!
Read this great tutorial that uses our bulk sms gateway.
fambi is offline
Reply With Quote
View Public Profile Visit fambi's homepage!
 
Old 01-31-2005, 04:07 PM
ACJavascript's Avatar
Humble Mod

Posts: 548
Location: CT, USA
I do not think so.

Functions are used to clean up and make code even faster.

If you had to repeat say:

ucwords(str_replace("_", " ", $category));

30 times becuase you had to check 30 variables it would take up alot more time and effort then if you used a function to return a TRUE or FALSE value.
__________________
CYTech-services.com - 100 Satisfied Customers - Custom Programming and Web Development
ACJavascript is offline
Reply With Quote
View Public Profile Visit ACJavascript's homepage!
 
Old 01-31-2005, 04:08 PM
fambi's Avatar
Ultra Talker

Posts: 339
Cool. Thanks for your input.
__________________
Sending sms from a website or application is easy!
Read this great tutorial that uses our bulk sms gateway.
fambi is offline
Reply With Quote
View Public Profile Visit fambi's homepage!
 
Old 01-31-2005, 09:59 PM
Christopher's Avatar
Iced Cap

Posts: 3,111
Location: Toronto, Ontario
The basic rule of thumb is, if you need to use the same code more then once within your script, then it belongs in a function. PHP going over the function once is faster then it going over, say, 400 lines twice.
Christopher is offline
Reply With Quote
View Public Profile Visit Christopher's homepage!
 
Old 02-01-2005, 05:25 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Does php do some degree of compilation then or is it an entirely interpreted language? If the latter, then all a function call does is jump to another place in your code and continue executing. It would be no faster or slower than writing it all inline, maybe with a couple of instructions worth of overhead to jump and store the return address.

You wouldn't get that far without functions let's face it!
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';
Scribble Pad MOD for phpBB (aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 02-01-2005, 08:25 AM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
0beron, you are right in that PHP is strictly interpretive and that the overhead of calling a function is minimal. So in comparison to a compiled module, the increased efficiency of function utilization is minimal. However, from a development, cognitive and maintenance standpoint, the value of functions and PHP objects are inestimable in that it provides developers the basis for constructing our own application frameworks.
__________________
—Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 02-01-2005, 02:00 PM
Christopher's Avatar
Iced Cap

Posts: 3,111
Location: Toronto, Ontario
You are right. Though Zend Optimiser and similar addons (such as Turck) reduce speed by "compiling" PHP, the overhead of functions is still small in some cases.

I just did an experiment to see how much overhead. I wrote two small functions and called them three times within a loop that executed them 100,000 times, then took the code from the functions and pasted it three times within another loop which also executed 100,000 times. When I outputted data (I was working with strings), the function calls were much faster by a factor of 6 seconds. When I enabled output buffering, the function calls were slower by about 0.8 seconds.
Christopher is offline
Reply With Quote
View Public Profile Visit Christopher's homepage!
 
Reply     « Reply to Putting together a function
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.14990 seconds with 12 queries