Variable Scope in Include Files

From: Tom (klenwell_at_gmail.com)
Date: 12/29/04


Date: 29 Dec 2004 02:19:37 -0800

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/language.variables.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



Relevant Pages

  • Re: Newcomer question wrt variable scope/namespaces
    ... value to a non-local dictionary/list member does NOT generate an " UnboundLocalError" exception and the assignment is preserved upon exiting that scope. ... This would "violate" the python scoping rules where a variable in a global scope can only be referenced to -- unless declared as "global". ... 'a' refers, ...
    (comp.lang.python)
  • Re: Newcomer question wrt variable scope/namespaces
    ... value to a non-local dictionary/list member does NOT generate an " UnboundLocalError" exception and the assignment is preserved upon exiting that scope. ... This would "violate" the python scoping rules where a variable in a global scope can only be referenced to -- unless declared as "global". ... 'a' refers, ...
    (comp.lang.python)
  • Re: "x.constructor == Foo" vs "x instanceof Foo"
    ... The second declaration for `a' overwrites the reference with one to another ... Javascript variables that aren't declared in a function scope ... named functions defined in the global scope ...
    (comp.lang.javascript)
  • Re: strtok & global variables
    ... While ``Global'' refers to scope, ... file-scope and its linkage is external. ... the appearance that the scope of a particular ...
    (comp.lang.c)
  • Re: typedef and static
    ... Well of course functions cannot have function scope. ... An identifier is visible only within a region ... declarator or type specifier that declares the identifier appears ...
    (comp.lang.c)