Jump to content

Archived

This topic is now archived and is closed to further replies.

master82

Functions

Recommended Posts

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 across

Thanks for your time  ;)

Share this post


Link to post
Share on other sites
The first place to look should always be the manual:

http://www.php.net/functions

Be sure to read the sub sections as well.

If you still have questions, please ask after reading the manual.

Share this post


Link to post
Share on other sites
Sorry, should have said - I looked there and it made no sense to me! lol

Thats why I was hoping someone could put an easy/simple example of a function they have used then explain exactly what is happening.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ?)

Share this post


Link to post
Share on other sites
include the file that the function is in, then call it like any other php function:

[code]foo("this is a test");[/code]

Share this post


Link to post
Share on other sites
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);
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.