Any variable you want your function to use you need to declare as an attribute in the normal brackets.
For example:
/////
function product ($a, $b) {
$result = $a * $b;
return $result;
}
/////
You can call the function like this:
/////
$p = product(5, 3);
print $p;
/////
15 will be printed
Or like this:
/////
$a = 5;
$b = 3;
$p = product($a, $b);
print $p;
/////
15 will be printed
Note that all variables inside the functions are different to the ones outside of the functions. This is correct for other functions as well.
So we can have:
/////
$result = "Old data";
$a = 5;
$b = 3;
$p = product($a, $b);
print $p;
print $result;
/////
15 will be printed, then "Old data" will be printed (sans quotation marks). The $result INSIDE the function will not overwrite the $result OUTSIDE the function.
Now if you want your function to share some variables with the outside world, you can tell it to do so like this:
/////
function product ($a, $b) {
global $c;
$result = $a * $b + $c;
return $result;
}
/////
Looking back at our script, we can do this:
/////
$a = 5;
$b = 3;
$c = 10;
$p = product($a, $b);
print $p;
/////
25 will be printed, as the function will read $c from outside. However, do this sparingly as making things GLOBAL can be both a security threat and it can intefere with other fuctions and be rather hard to trace, if it causes problems. Make sure you know at all times what variables you are making GLOBAL.
Variables you have "defined" will be read inside functions by default.
Like things you have made with:
define ('MAIN_TABLE', 'main');
can be read inside functions right off the bat.
Another nifty basic thing you can do with functions is this:
/////
function product ($a, $b=2) {
$result = $a * $b;
return $result;
}
/////
Notice I put $b=2 in the first line of the function. This forces the function to assign $b to be 2 as a default.
With the old fuction if you called it like this:
$p = product($a);
Then php will whinge about "expecting another parameter for product".
With the new function $b will become 2 if the script did not provide a variable to set $b.
/////
$a = 5;
$b = 3;
$p = product($a);
print $p;
/////
10 will be printed.
Assigning defaults to your attributes is a good habit, and have proven to save me some time, both in troubleshooting and in function usage.