Page 1 of 1

effeciency of global variable vs includes in a function?

Posted: Fri Jun 13, 2003 2:28 pm
by Swede78
Let's say I have a page with 10 functions. Each function uses $IncVar from an included file.

What's a better way of bringing in $IncVar so that that it works as a local scope variable? Should I include the file once at the top of the page, and define $IncVar as a global variable within each function, or should I include the file within each function?

I figure that it probably doesn't make much of a difference if I only use one of the functions. But in my case, I will be using most of the functions in one script. So, I wasn't sure which method would be more effecient.

Just trying to learn what works better. Thanks.

Posted: Fri Jun 13, 2003 2:34 pm
by Swede78
Oh, also... why doesn't "require_once" work in a function? It only lets me use "include".

Re: effeciency of global variable vs includes in a function?

Posted: Fri Jun 13, 2003 3:05 pm
by BDKR
Swede78 wrote: What's a better way of bringing in $IncVar so that that it works as a local scope variable? Should I include the file once at the top of the page, and define $IncVar as a global variable within each function, or should I include the file within each function?
Define the vars within the function. That's the fastest way of doing. My benchmarks show that to be the case and Sterling Hughes (one of the PHP developers) states as much. Including a file for each function call is not needed.

Cheers,
BDKR

Posted: Sat Jun 14, 2003 9:56 pm
by McGruff
Definitely agree with BD.

But.. do you need a var or would a constant do? Constants have advantages: immune from value substitution (well, they are AFTER they are defined) and you don't need to global them in anywhere.

Posted: Mon Jun 16, 2003 1:50 pm
by Swede78
Thanks BDKR and McGruff,

As far as variable vs constant. Actually, constants would work fine, I never change the values (at least within the same script). I'll define them as constants and see how that works. I don't ever use constants, so I'll need to read up on them. Thanks!