Forum Moderators: coopster

Message Too Old, No Replies

Question about declaring globals within a function

is there a downside to declaring too many globals

         

russkern

12:10 pm on Mar 13, 2008 (gmt 0)

10+ Year Member



If I have written a function that basicly extracts data from a table and then declare globals for all the data, for example:
global $fname=$row[1];
global $lname=$row[2];
etc, etc. maybe up to 12 variables)

Is there a downside to declaring all those global variables at any given time if I'm not using them all? Keeping in mind that on one page I may use a few of them, and on another page use a different few.

Just a question I've had and I just saw mention of globals from within functions on a different thread which prompted me to finally ask.

R

coopster

2:07 pm on Mar 13, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It will start to get confusing trying to remember where and what variables you declared global and hopefully you don't start accidentally overwriting them in your other functions or included files because you didn't realize variables with the same names already exist in the scope.

If you are finding that you are using globals more and more often have you considered object oriented programming [php.net]?

russkern

5:46 pm on Mar 13, 2008 (gmt 0)

10+ Year Member



LOL... I'm just getting comfortable with the ins & Outs of Functions...However, if OBject Oriented programing is where I need to head then that's what I'll do.

As to the Globals thing... I don't have SOO many that it would start getting confusing, And I have a pretty good method for keeping them seperated, but it is easy to see how a person could trip him/her self up with too many of them floating around.

Thanks... Russ

coopster

6:23 pm on Mar 13, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



There's nothing wrong with declaring and using global variables, so don't misunderstand me. If you are more comfortable with that concept than go with it for now. I was just offering some advice, since you asked ;)

russkern

7:12 pm on Mar 13, 2008 (gmt 0)

10+ Year Member



Not a problem... advice IS what I asked for...

Thanks for the info.... I started looking at Object Oriented Programming so we'll see what happens... I may stick with globals in Functions for now however...

Thanks Much.

R