AV1611 Posted May 17, 2007 Share Posted May 17, 2007 I notice a lot of scripts where the author will create a function that only gets used once. Why bother? if it's a one time deal, why not just run the code? I'm looking for the reason. Is it just to make the code look better? Does is somehow make the script faster? Is it just "best practice"? I'm just trying to learn... Quote Link to comment https://forums.phpfreaks.com/topic/51830-solved-when-to-use-functions/ Share on other sites More sharing options...
thedarkwinter Posted May 17, 2007 Share Posted May 17, 2007 ... any idea how often you write a huge piece of code,and then later discover you need to use it again, so you copy and paste it, and then you realize you need to use it yet again! Functions are good practice, they are neater, but they wont speed things up or anything. Quote Link to comment https://forums.phpfreaks.com/topic/51830-solved-when-to-use-functions/#findComment-255400 Share on other sites More sharing options...
taith Posted May 17, 2007 Share Posted May 17, 2007 sometimes run faster, and not always better practice... but it does make programming it faster... for example... every function in itself is a "program"... <?php function db_connect($host, $username, $password, $database){ global $dblink; $dblink=@mysql_connect("$host","$username","$password"); $dbdatabase=@mysql_select_db("$database"); if((!$dblink)||(!$dbdatabase)) return false; register_shutdown_function('db_disconnect'); return true; } ?> i only use that once on every page, but, as you see... just by db_connect("","","",""); it automatically connects to the host, returns false on error, turns on the db_disconnect, and returns true... i use functions ALOT... but thats just my style... i find that by using functions, i can expand what i'm doing with alot less code... Quote Link to comment https://forums.phpfreaks.com/topic/51830-solved-when-to-use-functions/#findComment-255403 Share on other sites More sharing options...
trq Posted May 17, 2007 Share Posted May 17, 2007 Functions can help to keep your code cleaner, even if its just because they don't introduce extra variables into the current namespace. Also, sometimes it just easier to put logic into a function. There is no real straight up answer here. IMO, the more code within functions the better, however, I never use functions to output html. My functions always return data to the calling code, which then prints the result. eg; <?php function hello() { return "Hello world"; } echo hello(); ?> Not <?php function hello() { echo "Hello world"; } hello(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/51830-solved-when-to-use-functions/#findComment-255404 Share on other sites More sharing options...
envexlabs Posted May 17, 2007 Share Posted May 17, 2007 i was actually just wondering this last night aswell, about using functions and if they speed up scripts. Quote Link to comment https://forums.phpfreaks.com/topic/51830-solved-when-to-use-functions/#findComment-255405 Share on other sites More sharing options...
taith Posted May 17, 2007 Share Posted May 17, 2007 depending on the design of the function, yes they can speed up your scripts but they make life ALOT easier in anysense... lol Quote Link to comment https://forums.phpfreaks.com/topic/51830-solved-when-to-use-functions/#findComment-255409 Share on other sites More sharing options...
AV1611 Posted May 17, 2007 Author Share Posted May 17, 2007 Thank you for all the fine answers. Much to ponder. And Yes, I've had to copy and past a ton of code over and over. Usually it's because the customer spec keeps changing as the app is developed... Quote Link to comment https://forums.phpfreaks.com/topic/51830-solved-when-to-use-functions/#findComment-255433 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.