I'll see if I can give you a small rundown on them here:
Functions are a way to easily be able to re-use code and organize your programs better. They are very simple to use and help greatly, lol i already sound liek a textbook...
The basic function definition in php looks like this:
[B]function[/B] functionName ([I]optional_args[/I]) {
<code>
}
Lets take that and make a function that draws a rectangle:
function drawQuadrilateral() {
echo "****************<br>\n";
echo "* *<br>\n";
echo "* *<br>\n";
echo "****************<br>\n";
}
Now, if you
echo drawQuadrilateral();
you will see all those *'s print out in that shape.
However that is not very flexible and easy to change, lets make the same function but have it take on arguments (parameters) to make the dimentions.
function drawQuadrilateral($height,$width)
{
for($current = 0; $current < $width; $current++) {
echo '*';
}
echo "<br>";
for($col = 0; $col <= $height - 2; $col++) {
echo '*';
for($row=0; $row < $width * 2 - 4; $row++) {
echo ' ';
}
echo "*<br>\n";
}
for($current = 0; $current < $width; $current++) {
echo '*';
}
echo "<br>";
}
now try
echo drawQuadrilateral(5,30);
and
echo drawQuadrilateral(10,15);
and see the output..
How about a function that returns a value: These are very easy..
Here is a function that will calculate the sum of squares from a number down to 1. i.e. if they enter 5, it will calculate 55 + 44 + 33 + 22..
function sumSquares($number)
{
for ($current = $number; $current >= 1; $current--) {
$sumSquares = $sumSquares + ($current * $current);
}
return $sumSquares;
}
As you can see, these functions are not too hard...with return functions, you can return any kind of data-type, arrays, booleans, ints, floats, etc...
Lets close with a few notes:
What variables are accessable to functions?
The simple answer is: none. Only variables which are passed into functions thru parameters are accessable, but there is a way around this. In PHP, all variables outside of different functions are kept in the $_GLOBAL array. So right inside the function, you can get variables to be accessable from within. To do this, simply put
global $varname, $varname2, $varname3...;
Then those variables are accessable to the function without being passed in.
Now you may wonder, if you have seen on the php manual or in functions where there are optional variables that you can enter if you choose but are not mandatory. This is done by creating a default value for them in the function definition. See the following:
function optionalVars($height, $width, $color = "#ff0000") {
...
With a function like that, you can call it in multiple ways
First like
optionalVars(5,12);
or
optionalVars(5,12,"#ff00ff");
either will work and if the third parameter is blank, it will default to #ff0000.
For more information on functions, see the official php.net website documentation on functions located at http://php.net/manual/en/functions.php
If you have more questions feel free to email or post back....i hope that helped you out some..