Hi Edd, I think you are missing some critical points of information:
Variable scope: A variable that 'lives' in a function, is not available outside the function. Try this:
function SetA($value)
{
$a = $value;
return($a);
}
$a = '';
$b = SetA(9);
echo "a is now: ". $a "--<br />";
echo "b is now: ". $b "--<br />";
This is why you need the return. $a in the main script is not affected by whatever we did in the function.
Then, why go through the hassle of writing a function?
Consider you have 5 pages, each of which needs to get a menu. The menu is based on a call to the database, with settings sepcific for the page you are on. So you could in theory use a script which you copy into each page (Or better: include on each page). However, that would require you to use exactly he same variable names on each page, which is not alway usefull. Of, you could create a function, build_menu(), which takes a number of arguments (Such as an array with menu items, their status and link). The latter would make your code easier to read (After all, you can tell exactly where the menu is forme, and which data was used as input). Also, it makes it easier to re-use code over and over again in the same page.
For instance: I wrote a script 'get_variable'. This script checks the POST array and the GET array whether a variable is set. Then I have told the script to find an stringarray, a numeric array, a string or a number as a value. If the variable does not match the content I expect, it sets a default value. All in all, some 20 lines of code. So now, if I want to grab variables which may be passed by a form OR by a URL reference, I just go:
== function get_variable($label, $type, $default) ==
$variable = get_variable('SomeName', 1, '');
$variable2 = get_variable('SomeName2', 1, '');
$variable3 = get_variable('SomeName3', 1, '');
And my variables are verified, secured for further queries and I am ready to go.