Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Am I using functions correctly

Status
Not open for further replies.

georgeocrawford

Technical User
Aug 12, 2002
111
GB
Hi,

Just wondering if I am going about this the best way.

I have divided a fairly extensive script up into functions, firstly to prevent having to print the same code twice, and secondly to help me keep control of my script.

The problem is that, for the vast majority of my functions, most of the variables inside have to be carried out, and visa versa. This means that I am having to declare a huge number of variables as 'global' in each function:

Code:
function process_something() {

global $variable1, $variable2, $variable3, $variable4..... ;
...function continues

Code:
}

Of course this becomes even more ridiculous when I call a function from inside another function, because I have to declare all the shared variables as 'global' in both the 'child' and the 'parent' functions.

I can see two possible solutions.

1) Some php function I haven't yet heard of which declares a set of variables as 'global' for the whole script (i.e. every constituent part of it, including all the functions).

2) Change the structure of my script - perhaps using classes (which I haven't yet learnt about).


Thanks for any help. ______________________

George
 
Usually what you're describing is a result of not functionalizing properly.

Global variables should be few and far between.

One thing you could do, if you're happy with the functions as is, is to pass the variables to the functions.

function add($a, $b) {
$c=$a + $b;
return $c;
}

$one=1;
$two=2;

$three = add($one, $two);

or if you want to manipulate variables within a function, pass them by reference.

function append(&$string, $sub_str) {
$string .= $sub_str;
}

in which case the following code
$my_string = "hello";
$next_string = " world";

append($my_string, $next_string);
echo $my_string;

would print out
hello world

The proper solution is probably some combination of the two, with one main function (or the main part of your script, this is a scripting language afterall) holding most of the variables and passing them around properly.

The last thing to keep in mind is the $_GLOBALS array, which would give you closer functionallity to what you were actually asking about (sorry didn't think of it until I started typing)

But basically you can put all your variables in an array, declare that array as global, and everything get's passed.

so

$_GLOBAL["variable1"]
$_GLOBAL["variable2"]

global $_GLOBAL;

(I think there is a special array called either $_GLOBAL or $_GLOBALS which is automatically declared as global everywhere, but you'll have to look into that)

-Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top