Forum Moderators: coopster

Message Too Old, No Replies

setting a value to be accessible throughout my script

constants/globals accessing them?

         

Matthew1980

2:07 pm on Jan 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello there people of webmasterworld!

Hope everyone had a good new year!

I would like some clarity on constants and globals please; I know how to declare a constant:-

define('NAME', "matthew");

a constant called "matthew",

Globals, from what I can understand is simply:-

global $variable_name;

from within a function, literally asking php to look outside the function to find a variable called $variable_name.

My issue is, I have a constant set in one area and I would like to call this constant in another area, without having to re-declare it, whether this is the done thing or not i'm not sure? So now I am thinking could I set a constant as global and if so how do I do it, my aim is to just have this constant available throughout my script?

If anyone understands my request I would appreciate the help.

Cheers,

MRb

dreamcatcher

3:22 pm on Jan 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Constants are global in scope, so this:

define('NAME', "matthew");

is available anywhere in your script, including functions and classes.

dc

Matthew1980

3:45 pm on Jan 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey there DC,

I think I have understood why I need to have the Constant re-defined, as the script assigns variables and constants in the flow of the document, the first instance will not have been set if I use, in this case the administration index.php file which is 1 directory down from the main index parsing file, in which the original constant is defined, and of course if the main parsing file isn't in use, the constant wont have been defined thus the value is not set.

I hope I make sense, and obviously I hope I'm right, which is possible that I am not..

Cheers for the help though DC.

MRb

FourDegreez

4:12 pm on Jan 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a small file declaring a few globals and modifying the include path that I include at the top of every script.

Matthew1980

5:13 pm on Jan 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there FourDegreez,

I have since thought about the problem and have done basically what you have suggested and now everything functions as required. Simple, often the answers to many problems are.. I should get a T-Shirt with that on!

Anyway guys, thanks once again for the advice.

MRb