Forum Moderators: coopster

Message Too Old, No Replies

can I make a variable always global?

so I don't have to declare it global in every function

         

pixeltierra

7:22 pm on Aug 16, 2006 (gmt 0)

10+ Year Member



I'm working on a series of "constants" that I use all the time on my pages. But every function that uses them needs them to be made global. Is there a way around this?

Thanks all.

pixeltierra

8:06 pm on Aug 16, 2006 (gmt 0)

10+ Year Member



I've found the superglobal $GLOBALS can do the job. Is there any reason not to use this?

smatts9

9:07 pm on Aug 16, 2006 (gmt 0)

10+ Year Member



I am pretty sure superglobals are insecure.

pixeltierra

9:38 pm on Aug 16, 2006 (gmt 0)

10+ Year Member



I have registerglobals turned off but $GLOBALS still works.

I think the config option for globals you're thinking of has to do with register globals. A GET or POST variable automatically become a variable without specifically getting like this:

$var = $_GET['var'];

If registerglobals is on than this: url/file.php?var=foo
automatically maks a variable called $var with value "foo".

This is as far as I know. Someone correct me if I'm wrong, or if there are other insecurities involved with using $GLOBALS.

whoisgregg

10:59 pm on Aug 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To my knowlege, both your solution of using $GLOBALS and your explanation of register_globals are sound. :)

For reference, $GLOBALS is talked about on the PHP reserved variable page [us2.php.net] and a good explanation of the security risk of register_globals is here [us2.php.net].