linux1880 Posted January 15, 2010 Share Posted January 15, 2010 Hi friends, what is the best way to write function statesments ? Like if i am writing function what should i keep in mind ? Is there any magical tactis or formula ? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/188517-what-is-the-best-way-to-write-statesment-in-the-function/ Share on other sites More sharing options...
premiso Posted January 15, 2010 Share Posted January 15, 2010 The best way is to use them when needed, you can over function a program. But really there is no "standard" for writing functions. It is more or less to get rid of redundant code, so if say you need to constantly grab data for different ID's you can make a grabData(ID) function which grabs the data for a specific ID, etc. But if you have specific ideas, why not share what you are thinking and attempt to write one for what you are thinking and ask for that to be critiqued. Quote Link to comment https://forums.phpfreaks.com/topic/188517-what-is-the-best-way-to-write-statesment-in-the-function/#findComment-995275 Share on other sites More sharing options...
JAY6390 Posted January 15, 2010 Share Posted January 15, 2010 Yeah basically small snippets of reusable code are what functions are. If you see something you think can be used again somewhere else in your script, make it a function. I'd say have a minimum of 3 executable lines of code as a minimum. I've seen people use functions to shorten a function name, and while it's handy, it's hardly needed really examples function pr($expression, $return = false) { return print_r($expression, $return); } function mres($data) { return mysql_real_escape_string($data); } They're kinda handy for the users that don't have a good PHP ide but for those that do it's pointless with intelli code popups, snippet shortcuts and auto replaces Quote Link to comment https://forums.phpfreaks.com/topic/188517-what-is-the-best-way-to-write-statesment-in-the-function/#findComment-995306 Share on other sites More sharing options...
oni-kun Posted January 15, 2010 Share Posted January 15, 2010 function mres($data) { return mysql_real_escape_string($data); } Your mres() is really just an alias the way you put it, albeit it does look better to type a few hundred times than the former. A good example if they need to run multiple checks such as simple validation. Quote Link to comment https://forums.phpfreaks.com/topic/188517-what-is-the-best-way-to-write-statesment-in-the-function/#findComment-995309 Share on other sites More sharing options...
JAY6390 Posted January 15, 2010 Share Posted January 15, 2010 Yeah, and so is the pr, I Just dont see the point in typing up a function for something so minimal Quote Link to comment https://forums.phpfreaks.com/topic/188517-what-is-the-best-way-to-write-statesment-in-the-function/#findComment-995310 Share on other sites More sharing options...
x_filed2k Posted January 15, 2010 Share Posted January 15, 2010 Sorry wrong post-reply area. Quote Link to comment https://forums.phpfreaks.com/topic/188517-what-is-the-best-way-to-write-statesment-in-the-function/#findComment-995312 Share on other sites More sharing options...
JAY6390 Posted January 15, 2010 Share Posted January 15, 2010 Are you sure you have the right thread? Nobody has mentioned columns or tables lol Quote Link to comment https://forums.phpfreaks.com/topic/188517-what-is-the-best-way-to-write-statesment-in-the-function/#findComment-995313 Share on other sites More sharing options...
linux1880 Posted January 15, 2010 Author Share Posted January 15, 2010 Thank you so much friends . I have seen lot's of builtin php function has optional arguments. How do we write optional arguments ? Quote Link to comment https://forums.phpfreaks.com/topic/188517-what-is-the-best-way-to-write-statesment-in-the-function/#findComment-995440 Share on other sites More sharing options...
trq Posted January 15, 2010 Share Posted January 15, 2010 Take a look at the manual, functions. Quote Link to comment https://forums.phpfreaks.com/topic/188517-what-is-the-best-way-to-write-statesment-in-the-function/#findComment-995444 Share on other sites More sharing options...
oni-kun Posted January 15, 2010 Share Posted January 15, 2010 Thank you so much friends . I have seen lot's of builtin php function has optional arguments. How do we write optional arguments ? It's generally a good idea to read up on the manual, as they provide samples and useful user contributed comments which are relevant to many problems. Here is a very simple example: function myFunction ($display, $bold=false) { if ($bold == true) { echo "<b>" . $display . "</b>"; } else { echo $display; } } myFunction ('This is a string! '); myFunction ('This is a bold string' , true); Quote Link to comment https://forums.phpfreaks.com/topic/188517-what-is-the-best-way-to-write-statesment-in-the-function/#findComment-995470 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.