master82 Posted August 13, 2006 Share Posted August 13, 2006 I've just come across some code that had alot of functions within it - can somebody please try to explain the purpose of these and maybe give a practicle example to get the point acrossThanks for your time ;) Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted August 13, 2006 Share Posted August 13, 2006 The first place to look should always be the manual:http://www.php.net/functionsBe sure to read the sub sections as well.If you still have questions, please ask after reading the manual. Quote Link to comment Share on other sites More sharing options...
master82 Posted August 13, 2006 Author Share Posted August 13, 2006 Sorry, should have said - I looked there and it made no sense to me! lolThats why I was hoping someone could put an easy/simple example of a function they have used then explain exactly what is happening. Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted August 13, 2006 Share Posted August 13, 2006 The point of a function is to wrap some lines of code that you plan on using more than once into a single package. This way if you need to change that code, you only have to do it once, rather than digging through all of your lines of code to find every instance.For example:[code]function foo($arg1) { echo $arg1;}[/code]Very simple function, just echo's out what it is passed. You could use that function to output all of your data to the browser, for example. Now, if you weren't using that function, and you wanted to make all text output to the user red, you would have to dig through your code and put a style tag around each one. But with the function it's as easy as:[code]function foo($arg1) { echo '<span style="color: red;">' . $arg1 . '</span>';}[/code]And now all text will be red. Quote Link to comment Share on other sites More sharing options...
master82 Posted August 13, 2006 Author Share Posted August 13, 2006 Oh... Im starting to see it now.Am i right in thinking that foo is the name of the function?How would I call that function if it was on a different php page?(use require page.php and use $foo ?) Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted August 13, 2006 Share Posted August 13, 2006 include the file that the function is in, then call it like any other php function:[code]foo("this is a test");[/code] Quote Link to comment Share on other sites More sharing options...
extrovertive Posted August 13, 2006 Share Posted August 13, 2006 a function is nothing more than an equation.x = 5;x = 2 *x;x is now (2 *5 ) which is 10.$x = 2;echo timestwo($x);function timestwo($x){ return (2 * $x);} Quote Link to comment Share on other sites More sharing options...
ScottRiley Posted August 13, 2006 Share Posted August 13, 2006 Generally, the purpose of a function is to prevent the repetition of code. You create a function to perform certain task that could be extremely inefficient to type out over and over again. where a simple example of a function would be to add 1 to a number, function addone($number){ $number++;}This could be called like '$newnumber = addone($oldnumber)', which is actually less efficient to type thatn $oldnumber++...howver, it illustrates the use of a function. Now, consider having to echo a large amount of HTML, this could exceed 200 lines. Say, for example, you needed to show this HTML whenever a user-error occured (e.g. they didn't fill in a field in a form, they chose an incorrect answer in a quiz). Now, without functions, for every error that occured, you would have to copy and paste the echo statement for every error. This means your code could easily exceed 1,000 lines. Now, consider this with functions. You declare the function, in which you have the echo statement, then you just call the function ater every error. this means, instead of 200 lines of code for every error, there is potentially just one.Also, assume you made an error in the echo statement, if you had copied and pasted the statement, you would have to ammend the statement for each error. If you used a single function, you would need to edit this once. Quote Link to comment 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.