I'm tying myself in knots trying to figure out variable scope with
constants and include files. This is what I'm doing:
A page (index.php) on my website includes a general purpose include
file (ini.inc) that declares a constant DAY_NUM. A little later in the
file, ini.inc includes another file (b_data.inc) that tries to use
DAY_NUM. I'm getting a "Use of Undefined Constant" notice as a result
of this line when I load index.php in the browser.
The index.php also refers to DAY_NUM after the include(ini.inc)
statement without a problem. When I move the DAY_NUM constant
declaration statement to b_data.inc, I get rid of the first "Undefined
Constant" notice, but then I get a new one where index.php refers to
DAY_NUM.
(I hope that makes sense.)
I was assuming that any constant had global scope and therefore when I
declared it in my ini.inc file, any subsequent script or files included
would have access to it. But that appears not to be the case. Is this
counter-intuitive, or am I missing something here?
I found this note on the PHP site
(http://us2.php.net/manual/en/languag...les.scope.php), but it's a
bit vaguely worded and doesn't refer specifically to constants:
"if you define a function in that included file, it can't get the
variables from the inluded file's scope. global won't work. The only
way to give such an include function access to global vars is via
arguments. I don't know if this is a bug in PHP."
And I'm getting the notice even when I refer to the constant outside a
function in the include file.
If it is the case that constants declared in an included file don't
have global scope, please let me know, as I'll have to rethink my site
architecture.
Thanks,
Tom